{"id":1254,"date":"2013-09-26T22:48:28","date_gmt":"2013-09-27T02:48:28","guid":{"rendered":"http:\/\/yuguangzhang.com\/blog\/?p=1254"},"modified":"2013-10-17T18:02:52","modified_gmt":"2013-10-17T22:02:52","slug":"using-encapsulation-to-make-an-awesome-angular-js-editor-in-fiddle-salad","status":"publish","type":"post","link":"http:\/\/yuguangzhang.com\/blog\/using-encapsulation-to-make-an-awesome-angular-js-editor-in-fiddle-salad\/","title":{"rendered":"Using Encapsulation to Make an Awesome Angular JS Editor in Fiddle Salad"},"content":{"rendered":"<p>Angular JS is a popular MVC framework by Google, and I received <a href=\"https:\/\/github.com\/yuguang\/fiddlesalad\/issues\/25\">a bug report that Fiddle Salad wouldn&#8217;t reload the preview<\/a> as it does in all other cases. This problem was noticed before, but it wasn&#8217;t a priority since pressing the refresh button as required in all other web development methods solves the problem. However, this was because I was attempting to solve it at the wrong level by modifying a method. As I&#8217;ve learned, there are dependencies between the execute, reset, and initiation of the class involved. <\/p>\n<p>Fiddle Salad&#8217;s CodeRunner class has just 5 public methods. They are<\/p>\n<ul>\n<li>execute()<\/li>\n<li>add_file()<\/li>\n<li>remove_css()<\/li>\n<li>reset()<\/li>\n<li>debug()<\/li>\n<\/ul>\n<p>as in the class diagram on the wiki at\u00a0<a href=\"https:\/\/github.com\/yuguang\/fiddlesalad\/wiki\/2.-Classes\">https:\/\/github.com\/yuguang\/fiddlesalad\/wiki\/2.-Classes<\/a>. Therefore, any implementation of a CodeRunner just needs to support these 5 methods. The debug method could be refactored into a super class because it would generate the template for the new execute() method. <\/p>\n<p>[cc lang=&#8221;coffeescript&#8221;]<br \/>\ndebug: -><br \/>\n  ###<br \/>\n  Debug opens a new window with the code loaded in the page. External CSS and JS files are loaded through head tags.<br \/>\n  It assumes all external resources are stored in the view model.<br \/>\n  ###<br \/>\n  template =<br \/>\n    css: _.template &#8216;<link rel=\"stylesheet\" type=\"text\/css\" href=\"<%= source %>&#8221; \/>&#8217;<br \/>\n    js: _.template &#8216;<script type=\"text\/javascript\" src=\"<%= source %>\"><\/script>&#8216;<br \/>\n    html: _.template &#8220;&#8221;&#8221;<br \/>\n          <!DOCTYPE html><br \/>\n          <html><br \/>\n            <head><br \/>\n              <title>Fiddle Salad Debug View<\/title><br \/>\n              <script src=\"http:\/\/leaverou.github.com\/prefixfree\/prefixfree.min.js\"><\/script><\/p>\n<style>\n                <%= css %>\n              <\/style>\n<p>            <\/head><br \/>\n            <body><br \/>\n              <%= body %><br \/>\n              <%= headtags %><br \/>\n              <script type=\"text\/javascript\">\n                <%= javascript %>\n              <\/script><br \/>\n            <\/body><br \/>\n          <\/html><br \/>\n          &#8220;&#8221;&#8221;<br \/>\n  # initialize array of head tags<br \/>\n  headTags = new Array<br \/>\n  # for each external resource in view model<br \/>\n  _.each viewModel.resources(), (resource) =><br \/>\n    # get the file type of the resource, call mapped template with resource, and append generated HTML to head tags<br \/>\n    headTags.push template[@filetype resource.source()](source: resource.source())<br \/>\n  headtags = headTags.join(&#8221;)<br \/>\n  # get JavaScript and CSS code from the engine<br \/>\n  javascript = engine.get_code LANGUAGE_TYPE.COMPILED_PROGRAM<br \/>\n  body = engine.get_code LANGUAGE_TYPE.COMPILED_DOCUMENT<br \/>\n  css = engine.get_code LANGUAGE_TYPE.COMPILED_STYLE<br \/>\n  # call the template for the window with the head tags and code<br \/>\n  html = template.html {javascript, css, body, headtags}<br \/>\n  # open window with generated HTML<br \/>\n  window.open = &#8216;data:text\/html;charset=utf-8,&#8217; + encodeURIComponent(html)<br \/>\n[\/cc]<br \/>\nIf I just change the last line, the debug method turns into a reset method.<br \/>\n[cc lang=&#8221;coffeescript&#8221;]<br \/>\n@window.location = &#8216;data:text\/html;charset=utf-8,&#8217; + encodeURIComponent(html)<br \/>\n[\/cc]<br \/>\nThe rest of the class still needs to be completed, but they are as obvious as appending to an array when adding JS\/CSS files. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Angular JS is a popular MVC framework by Google, and I received a bug report that Fiddle Salad wouldn&#8217;t reload the preview as it does in all other cases. This problem was noticed before, but it wasn&#8217;t a priority since pressing the refresh button as required in all other web development methods solves the problem. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[203,23],"tags":[196,194,195],"class_list":["post-1254","post","type-post","status-publish","format-standard","hentry","category-fiddle-salad","category-programming","tag-angularjs","tag-coffeescript","tag-dynamic-editing"],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/comments?post=1254"}],"version-history":[{"count":1,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1254\/revisions"}],"predecessor-version":[{"id":1447,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/posts\/1254\/revisions\/1447"}],"wp:attachment":[{"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/media?parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/categories?post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/yuguangzhang.com\/blog\/wp-json\/wp\/v2\/tags?post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}