-
-
Save jupiterjs/989117 to your computer and use it in GitHub Desktop.
Revisions
-
JupiterJS revised this gist
Jun 8, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -64,8 +64,8 @@ When a new class instance is created, it calls the class's <code>init</code> met } }); var payal = new Person("Payal"); assertEqual( payal.speak() , 'I am Payal.' ); ### Calling base methods -
JupiterJS revised this gist
Jun 8, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -64,8 +64,8 @@ When a new class instance is created, it calls the class's <code>init</code> met } }); var justin = new Person("Payal"); assertEqual( justin.speak() , 'I am Payal.' ); ### Calling base methods -
JupiterJS revised this gist
Jun 8, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is Yet every part of JavaScriptMVC can be used without every other part, making the library lightweight. Its Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. This chapter covers __only__ JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. The following describes each component: - <code>$.Class</code> - JavaScript based class system - <code>$.Model</code> - traditional model layer -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,7 +53,7 @@ If you want to create a sub-class, simply call the the base class with the sub-c ### Instantiation When a new class instance is created, it calls the class's <code>init</code> method with the arguments passed to the constructor function: $.Class('Person',{ init : function(name){ -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -293,7 +293,7 @@ Now the nextPrev plugin can set offset with reckless abandon: ### Defaults We can add default values to Paginate instances by setting the static <code>defaults</code> property. When a new paginate instance is created, if no value is provided, it initializes with the default value. $.Model('Paginate',{ defaults : { -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -271,7 +271,7 @@ There are a few ways to make the Paginate model more useful. First, by adding s Settter methods are model prototype methods that are named <code>setNAME</code>. They get called with the val passed to <code>model.attr(NAME, val)</code> and a success and error callback. Typically, the method should return the value that should be set on the model instance or call error with an error message. Success is used for asynchronous setters. The following paginate model uses setters to prevent negative counts the offset from exceeding the count by adding <code>setCount</code> and <codE>setOffset</code> instance methods. $.Model('Paginate',{ setCount : function(newCount, success, error){ -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,7 +39,7 @@ In the example above, instances of Animal have a <code>breathe()</code> method. var man = new Animal(); man.breathe(); If you want to create a sub-class, simply call the the base class with the sub-class's name and properties: Animal("Dog",{ wag : function(){ @@ -53,7 +53,7 @@ If you want to create a sub-class, simply call the the base class with the sub-c ### Instantiation When a new class is created, it calls the class's <code>init</code> method with the arguments passed to the constructor function: $.Class('Person',{ init : function(name){ @@ -83,7 +83,7 @@ to provide a more 'classy' greating: ### Proxies Class's callback method returns a function that has 'this' set appropriately (similar to [$.proxy](http://api.jquery.com/jQuery.proxy/)). The following creates a clicky class that counts how many times it was clicked: $.Class("Clicky",{ init : function(){ -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,7 @@ The download will come with minified and unminified versions of jQuery and the p ## Class JMVC's Controller and Model inherit from its Class helper - $.Class. To create a class, call <code>$.Class(NAME, [classProperties, ] instanceProperties])</code>. $.Class("Animal",{ breathe : function(){ -
JupiterJS revised this gist
May 30, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ JavaScriptMVC's naming conventions deviate slightly from the traditional [Model- JavaScriptMVC can be used as a single download that includes the entire framework. But since this chapter covers only the MVC parts, go to the [download builder](http://javascriptmvc.com/builder.html), check Controller, Model, and View's EJS templates and click download. The download will come with minified and unminified versions of jQuery and the plugins you selected. Load these with script tags in your page: <script type='text/javascript' src='jquery-1.6.1.js'></script> <script type='text/javascript' src='jquerymx-1.0.custom.js'></script> -
moschel revised this gist
May 29, 2011 . 1 changed file with 11 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -443,9 +443,9 @@ The following table details how to use the task model to CRUD tasks. error( jqXHR) ) -> taskDeferred</code></pre></td> <td><p>To create an instance of a model on the server, first create an instance with <code>new Model(attributes)</code>. Then call <code>save()</code>.</p> <p>Save checks if the task has an id. In this case it does not so save makes a create request with the task's attributes. Save takes two parameters:</p> <ul> <li><code>success</code> - a function that gets called if the save is successful. Success gets called with the <code>task</code> instance and the <code>data</code> returned by the server.</li> <li><code>error</code> - a function that gets called if there is an error with the request. It gets called with jQuery's wrapped XHR object.</li> </ul> @@ -461,8 +461,8 @@ The following table details how to use the task model to CRUD tasks. <td>Retrieves a single task from the server. It takes three parameters: <ul> <li><code>params</code> - data to pass to the server. Typically an id like: <code>{id: 2}</code>.</li> <li><code>success</code> - a function that gets called if the request is succesful. Success gets called with the <code>task</code> instance.</li> <li><code>error</code> - a function that gets called if there is an error with the request.</li> </ul> findOne returns a deferred that resolves to the task. @@ -476,16 +476,16 @@ The following table details how to use the task model to CRUD tasks. <td>Retrieves an array of tasks from the server. It takes three parameters: <ul> <li><code>params</code> - data to pass to the server. Typically, it's an empty object (<code>{}</code>) or filters: <code>{limit: 20, offset: 100}</code>.</li> <li><code>success</code> - a function that gets called if the request is succesful. Success gets called with an array of task instances.</li> <li><code>error</code> - a function that gets called if there is an error with the request.</li> </ul> findOne returns a deferred that resolves to an array of tasks. </td> </tr> <tr> <td>Update a task</td> <td><pre><code>task.attr('name','take out recycling'); task.save( success( task, data ), error( jqXHR) ) -> taskDeferred</code></pre></td> @@ -502,7 +502,7 @@ The following table details how to use the task model to CRUD tasks. <td><p>Destroys a task on the server. Destroy takes two parameters:</p> <ul> <li><code>success</code> - a function that gets called if the save is successful. Success gets called with the <code>task<code> instance and the <code>data</code> returned by the server.</li> <li><code>error</code> - a function that gets called if there is an error with the request.</li> </ul> Destroy returns a deferred that resolves to the destroyed task. @@ -514,7 +514,7 @@ The <code>Task</code> model has essentially become a contract to our services! ### Type Conversion Did you notice how the server responded with createdAt values as numbers like <code>1303173531164</code>. This number is actually April 18th, 2011. Instead of getting a number back from <code>task.createdAt</code>, it would be much more useful if it returns a JavaScript date created with <code>new Date(1303173531164)</code>. We could do this with a <code>setCreatedAt</code> setter. But, if we have lots of date types, this will quickly get repetitive. To make this easy, $.Model lets you define the type of an attribute and a converter function for those types. Set the type of attributes on the static <code>attributes</code> object and converter methods on the static <code>convert</code> object. @@ -540,9 +540,9 @@ Task now converts createdAt to a Date type. To list the year of each task, writ ### CRUD Events Model publishes events when an instance has been created, updated, or destroyed. You can listen to these events globally on the Model or on an individual model instance. Use <code>MODEL.bind(EVENT, callback( ev, instance ) )</code> to listen for created, updated, or destroyed events. Lets say we wanted to know when a task is created and add it to the page. After it's been added to the page, we'll listen for updates on that task to make sure we are showing its name correctly. We can do that like: Task.bind('created', function(ev, task){ var el = $('<li>').html(todo.name); -
moschel revised this gist
May 29, 2011 . 1 changed file with 11 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -260,9 +260,9 @@ Second, the logic protecting a negative offset or offset above the total count i JavaScriptMVC's model inherits from $.Class. Thus, you create a model class by inheriting from <code>$.Model(NAME, [STATIC,] PROTOTYPE)</code>: $.Model('Paginate',{ staticProperty: 'foo' },{ prototypeProperty: 'bar' }) There are a few ways to make the Paginate model more useful. First, by adding setter methods, we can limit what values count and offset can be set to. @@ -271,7 +271,7 @@ There are a few ways to make the Paginate model more useful. First, by adding s Settter methods are model prototype methods that are named <code>setNAME</code>. They get called with the val passed to <code>model.attr(NAME, val)</code> and a success and error callback. Typically, the method should return the value that should be set on the model instance or call error with an error message. Success is used for asynchronous setters. The following paginate model uses setters to prevent negative counts and offsets and to prevent the offset from exceeding the count. $.Model('Paginate',{ setCount : function(newCount, success, error){ @@ -293,7 +293,7 @@ Now the nextPrev plugin can set offset with reckless abandon: ### Defaults We can add default values to Paginate instances by setting the static defaults property. When a new paginate instance is created, if no value is provided, it will use the default value. $.Model('Paginate',{ defaults : { @@ -306,11 +306,12 @@ We can add default values to Paginate instances by setting the static defaults p setOffset : function(newOffset, success, error){ ... } }) var paginate = new Paginate({count: 500}); assertEqual(paginate.limit, 100); assertEqual(paginate.count, 500); This is getting sexy, but the Paginate model can make it even easier to move to the next and previous page and know if it's possible by adding helper methods. ### Helper methods @@ -343,7 +344,7 @@ Helper methods are prototype methods that help set or get useful data on model i } }) Thus, our jQuery widget becomes much more refined: $.fn.nextPrev = function(paginate){ this.delegate('.next','click', function(){ @@ -361,11 +362,11 @@ Thus jQuery widget becomes much more refined: ### Service Encapsulation We've just seen how $.Model is useful for modeling client side state. However, for most applications, the critical data is on the server, not on the client. The client needs to create, retrieve, update and delete (CRUD) data on the server. Maintaining the duality of data on the client and server is tricky business. $.Model is used to simplify this problem. $.Model is extremely flexible. It can be made to work with all sorts of services types and data types. This book covers only how $.Model works with the most common and popular type of service and data type: Representational State Transfer (REST) and JSON. A REST service uses urls and the HTTP verbs POST, GET, PUT, DELETE to create, retrieve, update, and delete data respectively. For example, a tasks service that allowed you to create, retrieve, update and delete tasks might look like: <table> <tr> -
moschel revised this gist
May 29, 2011 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -177,7 +177,7 @@ This is similar to how JavaScriptMVC's model layer works. ## Model JavaScriptMVC's model and its associated plugins provide lots of tools around organizing model data such as validations, associations, lists and more. But the core functionality is centered around service encapsulation, type conversion, and events. ### Attributes and Observables @@ -202,11 +202,11 @@ The paginate variable is now observable. We can pass it to pagination controls assertEqual( paginate.offset, 0 ); assertEqual( paginate.attr('limit') , 20 ); If we clicked the next button, we need to increment the offset. Change property values with <code>model.attr(NAME, VALUE)</code>. The following moves the offset to the next page: paginate.attr('offset',20); When paginate's state is changed by one control, the other controls need to be notified. You can bind to a specific attribute change with <code>model.bind(ATTR, success( ev, newVal ) )</code> and update the control: paginate.bind('offset', function(ev, newVal){ $('#details').text( 'Showing items ' + (newVal+1 )+ '-' + this.count ) @@ -218,7 +218,7 @@ You can also listen to any attribute change by binding to the <code>'updated.att $('#details').text( 'Showing items ' + (newVal+1 )+ '-' + this.count ) }) The following is a next-previous jQuery plugin that accepts paginate data: $.fn.nextPrev = function(paginate){ this.delegate('.next','click', function(){ @@ -251,9 +251,9 @@ Tthe following is a next-previous jQuery plugin that accepts paginate data: }) }; There are a few problems with this plugin. First, if the control is removed from the page, it is not unbinding itself from paginate. We'll address this when we discuss controllers. Second, the logic protecting a negative offset or offset above the total count is done in the plugin. This logic should be done in the model. To fix this problem, we'll need to add additional constraints to limit what values limit, offset, and count can be. We'll need to create a pagination class. ### Extending Model -
moschel revised this gist
May 29, 2011 . 1 changed file with 11 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,20 +26,20 @@ The download will come with minified and unminified versions of jQuery and the p ## Class JMVC's Controller and Model make use of their Class helper - $.Class. To create a class, call <code>$.Class(NAME, [classProperties, ] instanceProperties])</code>. $.Class("Animal",{ breathe : function(){ console.log('breathe'); } }); In the example above, instances of Animal have a <code>breathe()</code> method. We can create a new <code>Animal</code> instance and call <code>breathe()</code> on it like: var man = new Animal(); man.breathe(); If you want to create a sub-class, simply call the the base class with the sub-class' name and properties: Animal("Dog",{ wag : function(){ @@ -51,9 +51,9 @@ If you want to create a sub-class, simply call the the base class with the sub-c dog.wag(); dog.breathe(); ### Instantiation When a new class is created, it calls the class' <code>init</code> method with the arguments passed to the constructor function: $.Class('Person',{ init : function(name){ @@ -83,7 +83,7 @@ to provide a more 'classy' greating: ### Proxies Class' callback method returns a function that has 'this' set appropriately (similar to [$.proxy](http://api.jquery.com/jQuery.proxy/)). The following creates a clicky class that counts how many times it was clicked: $.Class("Clicky",{ init : function(){ @@ -103,7 +103,7 @@ Class's callback method return a function that has 'this' set appropriately (sim ### Static Inheritance Class lets you define inheritable static properties and methods. The following allows us to retrieve a person instance from the server by calling <code>Person.findOne(ID, success(person) )</code>. Success is called back with an instance of Person, which has the <code>speak</code> method. $.Class("Person",{ findOne : function(id, success){ @@ -133,6 +133,10 @@ Class provides namespacing and access to the name of the class and namespace obj Jupiter.Person.shortName; //-> 'Person' Jupiter.Person.fullName; //-> 'Jupiter.Person' Jupiter.Person.namespace; //-> Jupiter var person = new Jupiter.Person(); person.Class.shortName; //-> 'Person' ### Model example -
moschel revised this gist
May 29, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,9 +17,9 @@ JavaScriptMVC's naming conventions deviate slightly from the traditional [Model- ## Setup JavaScriptMVC can be used as a single download that includes the entire framework. But since this chapter covers only the MVC parts, go to the [download builder](http://javascriptmvc.com/builder.html), check Controller, Model, and View's EJS templates and click download. The download will come with minified and unminified versions of jQuery and the plugins you selected (plus their dependencies, like $.Class). Load these with script tags in your page: <script type='text/javascript' src='jquery-1.6.1.js'></script> <script type='text/javascript' src='jquerymx-1.0.custom.js'></script> -
moschel revised this gist
May 29, 2011 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,18 +2,18 @@ The following is a VERY rough draft of an article I am working on for Alex MacCa ## Introduction JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is nearly a comprehensive (holistic) front-end development framework, packaging utilities for testing, dependency management, documentation, and a host of useful jQuery plugins. Yet every part of JavaScriptMVC can be used without every other part, making the library lightweight. Its Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. This chapter covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. The following describes each component: - <code>$.Class</code> - JavaScript based class system - <code>$.Model</code> - traditional model layer - <code>$.View</code> - client side template system - <code>$.Controller</code> - jQuery widget factory JavaScriptMVC's naming conventions deviate slightly from the traditional [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#Concepts) design pattern. $.Controller is used to create traditional view controls, like pagination buttons and list, as well as traditional controllers, which coordinate between the traditional views and models. ## Setup -
moschel revised this gist
May 29, 2011 . 1 changed file with 7 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,16 +2,18 @@ The following is a VERY rough draft of an article I am working on for Alex MacCa ## Introduction JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is nearly a comprehensive (holistic) front-end development framework; packaging utilities for testing, dependency management, documentation, and a host of useful jQuery plugins. Yet, every part of JavaScriptMVC can be used without every other part, making the library lightweight. It's Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. This chapter only covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. The following describes each component: - <code>$.Class</code> - JavaScript based class system - <code>$.Model</code> - traditional model layer - <code>$.View</code> - client side template system - <code>$.Controller</code> - jQuery widget factory JavaScriptMVC's naming conventions deviate slightly from the traditional [Model-View-Controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#Concepts) design pattern. $.Controller is used to create traditional view controls, like pagination buttons and list. We'll also learn how to use $.Controller as a traditional controller, one that coordinates between the traditional views and models. ## Setup -
JupiterJS revised this gist
May 28, 2011 . 1 changed file with 0 additions and 200 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -558,203 +558,3 @@ The content for [$.View has been moved here](https://gist.github.com/996208) The content for [$.Controller has been moved here](https://gist.github.com/996204). -
JupiterJS revised this gist
May 28, 2011 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -119,7 +119,7 @@ Class lets you define inheritable static properties and methods. The following }) Person.findOne(5, function(person){ assertEqual( person.speak(), "I am Payal." ); }) ### Introspection @@ -370,15 +370,15 @@ A REST service uses urls and the HTTP verbs POST, GET, PUT, DELETE to create, re "id" : 2, "name" : "do the dishes", "acl" : "rw" , "createdAt": 1303173531164 // April 18 2011 }</code></pre></td> </tr> <tr> <td>Get a task</td><td>GET</td><td>/task/2</td><td></td><td><pre><code>{ "id" : 2, "name" : "do the dishes", "acl" : "rw" , "createdAt": 1303173531164 // April 18 2011 }</pre></code></td> </tr> <tr> @@ -393,7 +393,7 @@ A REST service uses urls and the HTTP verbs POST, GET, PUT, DELETE to create, re "id" : 2, "name" : "do the dishes", "acl" : "rw" , "createdAt": 1303173531164 // April 18 2011 }]</code> </pre> </td> @@ -403,7 +403,7 @@ A REST service uses urls and the HTTP verbs POST, GET, PUT, DELETE to create, re "id" : 2, "name" : "take out recycling", "acl" : "rw" , "createdAt": 1303173531164 // April 18 2011 }</pre></code></td> </tr> <tr> -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 3 additions and 90 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -549,99 +549,12 @@ Lets say we wanted to know when a task is created and add it to the page. And a }) ## $.View The content for [$.View has been moved here](https://gist.github.com/996208) ## $.Controller The content for [$.Controller has been moved here](https://gist.github.com/996204). -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 2 additions and 130 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -641,137 +641,9 @@ However, some people might not be using StealJS or want to delay loading templat The callback function will be called with the result of the rendered template and 'this' will be set to the original jQuery object. ### $.Controller The content for [$.Controller has been moved here](https://gist.github.com/996204). ## FuncUnit -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -183,9 +183,9 @@ Fortunately, JavaScriptMVC makes it easy to make any data observable. A great e - limit - the number of items to display - count - the total number of items We can model this data with JavaScriptMVC's $.Model like: var paginate = new $.Model({ offset: 0, limit: 20, count: 200 -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ The following is a VERY rough draft of an article I am working on for Alex MacCaw's @maccman's Book. It is very rough, but even now a worthwhile read. Suggestions / comments are very welcome! Please help me :-) ## Introduction JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is nearly a comprehensive (holistic) front-end development framework; packaging utilities for testing, dependency management, documentation, and a host of useful jQuery plugins. Yet, every part of JavaScriptMVC can be used without every other part, making the library lightweight. It's Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,10 +4,10 @@ JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is This chapter only covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. JavaScriptMVC's naming conventions deviate slightly from the traditional Model-View-Controller design pattern. The following describes each component: - <code>$.Class</code> - JavaScript based class system. - <code>$.Model</code> - traditional model layer - <code>$.View</code> - client side template system - <code>$.Controller</code> - jQuery plugin system that can serve as traditional controller or view. We'll see how $.Controller is used to create traditional view controls, like pagination buttons and list. We'll also learn how to use $.Controller as a traditional controller, one that coordinates between the traditional views and models. -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is This chapter only covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. JavaScriptMVC's naming conventions deviate slightly from the traditional Model-View-Controller design pattern. The following describes each component: - <code>$.Class</code> - a JavaScript based class system. - <code>$.Model</code> - a traditional model layer - <code>$.View</code> - a client side template system - <code>$.Controller</code> - a jQuery plugin system that can serve as traditional controller or view. -
JupiterJS revised this gist
May 27, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is nearly a comprehensive (holistic) front-end development framework; packaging utilities for testing, dependency management, documentation, and a host of useful jQuery plugins. Yet, every part of JavaScriptMVC can be used without every other part, making the library lightweight. It's Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. This chapter only covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. JavaScriptMVC's naming conventions deviate slightly from the traditional Model-View-Controller design pattern. The following describes each component: - <code>$.Class</code> - a JavaScript based class system. - <code>$.Model</code> - a traditional model layter -
JupiterJS revised this gist
May 26, 2011 . 1 changed file with 23 additions and 75 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -547,71 +547,11 @@ Lets say we wanted to know when a task is created and add it to the page. And a }) ## $.View - Client Side Templates JavaScriptMVC's views are really just client side templates. Client side templates take data and return a string. Typically, the strings are HTML intended to be inserted into the DOM. jQuery.View is a templating interface that takes care of complexities using templates: - Convenient and uniform syntax - Template loading from html elements and external files. @@ -620,24 +560,24 @@ We'll use $.Controller to build jQuery.View is a templating interface that takes - Caching of processed templates. - Bundling of processed templates in production builds. JavaScriptMVC comes pre-packaged with 4 different template engines: - EJS - JAML - Micro - Tmpl This tutorial uses EJS templates, but all the following techniques will work with any template engine with minor syntax difference. ### Basic Use When using views, you almost always want to insert the results of a rendered template into the page. jQuery.View overwrites the jQuery modifiers so using a view is as easy as: $("#foo").html('mytemplate.ejs',{message: 'hello world'}) This code: 1. Loads the template in file 'mytemplate.ejs'. It might look like: <h2><%= message %></h2> @@ -654,30 +594,38 @@ This code: You can use a template with the following jQuery modifier methods: $('#bar').after('temp.ejs',{}); $('#bar').append('temp.ejs',{}); $('#bar').before('temp.ejs',{}); $('#bar').html('temp.ejs',{}); $('#bar').prepend('temp.ejs',{}); $('#bar').replaceWidth('temp.ejs',{}); $('#bar').text('temp.ejs',{}); ### Loading from a script tag View can load from script tags or from files. To load from a script tag, create a script tag with your template and an id like: <script type='text/ejs' id='recipesEJS'> <% for(var i=0; i < recipes.length; i++){ %> <li><%=recipes[i].name %></li> <%} %> </script> Render with this template like: $("#foo").html('recipesEJS',recipeData) Notice we passed the id of the element we want to render. ### Packaging StealJS is JavaScriptMVC's dependency management and build system. Although not covered in this book, it's worth noting that StealJS is able to build templates into a production file. This makes the application load much faster as it doesn't have to wait for templates to load. ### Deferreds ### Asynchronous loading By default, retrieving requests is done synchronously. This is fine because StealJS packages view templates with your JS download. -
JupiterJS revised this gist
May 26, 2011 . 1 changed file with 96 additions and 81 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,9 +2,18 @@ JavaScriptMVC (JMVC) is an open-source jQuery-based JavaScript framework. It is nearly a comprehensive (holistic) front-end development framework; packaging utilities for testing, dependency management, documentation, and a host of useful jQuery plugins. Yet, every part of JavaScriptMVC can be used without every other part, making the library lightweight. It's Class, Model, View, and Controller combined are only 7k minified and compressed, yet even they can be used independently. JavaScriptMVC's independence lets you start small and scale to meet the challenges of the most complex applications on the web. This chapter only covers JavaScriptMVC's $.Class, $.Model, $.View, and $.Controller. JavaScriptMVC's naming conventions deviate slightly from the traditional Model-View-Controller design pattern. The following describe each component: - <code>$.Class</code> - a JavaScript based class system. - <code>$.Model</code> - a traditional model layter - <code>$.View</code> - a client side template system - <code>$.Controller</code> - a jQuery plugin system that can serve as traditional controller or view. We'll see how $.Controller is used to create traditional view controls, like pagination buttons and list. We'll also learn how to use $.Controller as a traditional controller, one that coordinates between the traditional views and models. ## Setup JavaScriptMVC can be used as a single download that includes its entire toolset. But this chapter covers only JavaScriptMVC's MVC parts. Instead of downloading the entire framework, go to the [download builder](http://javascriptmvc.com/builder.html), check Controller, Model, and View's EJS templates and click download. The download will come with minified and unminified versions of jQuery and the plugins you selected. Load these with script tags in your page: @@ -596,6 +605,92 @@ Model has the model list plugin to help with this. new ListWidget($("#briansList"), "brian" ); new ListWidget($("#justinsList"), "justin" ); ## $.View - Client Side Templates JavaScriptMVC's views are really just client side templates. Later, we'll use $.Controller to create traditional MVC views. Client side templates take data and create HTML. They are We'll use $.Controller to build jQuery.View is a templating interface that takes care of complexities using templates: - Convenient and uniform syntax - Template loading from html elements and external files. - Synchronous and asynchronous template loading. - Template preloading. - Caching of processed templates. - Bundling of processed templates in production builds. JavaScriptMVC comes pre-packaged with 4 different templates: - EJS - JAML - Micro - Tmpl And there are 3rd party plugins for Mustache and Dust. ### Use When using views, you almost always want to insert the results of a rendered template into the page. jQuery.View overwrites the jQuery modifiers so using a view is as easy as: $("#foo").html('mytemplate.ejs',{message: 'hello world'}) This code: 1. Loads the template a 'mytemplate.ejs'. It might look like: <h2><%= message %></h2> 2. Renders it with {message: 'hello world'}, resulting in: <h2>hello world</h2> 3. Inserts the result into the foo element. Foo might look like: <div id='foo'><h2>hello world</h2></div> ### jQuery Modifiers You can use a template with the following jQuery modifier methods: $('#bar').after('temp.jaml',{}); $('#bar').append('temp.jaml',{}); $('#bar').before('temp.jaml',{}); $('#bar').html('temp.jaml',{}); $('#bar').prepend('temp.jaml',{}); $('#bar').replaceWidth('temp.jaml',{}); $('#bar').text('temp.jaml',{}); ### Loading from a script tag View can load from script tags or from files. To load from a script tag, create a script tag with your template and an id like: <script type='text/ejs' id='recipes'> <% for(var i=0; i < recipes.length; i++){ %> <li><%=recipes[i].name %></li> <%} %> </script> Render with this template like: $("#foo").html('recipes',recipeData) Notice we passed the id of the element we want to render. ### Asynchronous loading By default, retrieving requests is done synchronously. This is fine because StealJS packages view templates with your JS download. However, some people might not be using StealJS or want to delay loading templates until necessary. If you have the need, you can provide a callback paramter like: $("#foo").html('recipes',recipeData, function(result){ this.fadeIn() }); The callback function will be called with the result of the rendered template and 'this' will be set to the original jQuery object. ## $.Controller - jQuery plugin factory JavaScriptMVC's controllers are really a jQuery plugin factory. They can be used as a traditional view, for example, making a slider widget, or a traditional controller, creating view-controllers and binding them to models. If anything happens in your application, a controller should respond to it. @@ -727,86 +822,6 @@ Controllers provide support for many types of special events. Any event that is Drag, drop, hover, and history and some of the more widely used controller events. These events will be discussed later. ## FuncUnit -
JupiterJS revised this gist
May 26, 2011 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ JMVC's Controller and Model are based on it's Class helper - $.Class. To create } }); In the example above, instances of Animal have a <code>breath()</code> method. We can create a new <code>Animal</code> instance and call <code>breathe()</code> on it like: var man = new Animal(); man.breathe(); -
JupiterJS revised this gist
May 26, 2011 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -494,6 +494,32 @@ The following table details how to use the task model to CRUD tasks. The <code>Task</code> model has essentially become a contract to our services! ### Type Conversion Did you notice how the server responded with createdAt values as numbers like <code>1303173531164</code>. This number is actually April 18th, 2011. Instead of getting a number back from <code>task.createAt</code>, it would be much more useful if it returns a JavaScript date created with <code>new Date(1303173531164)</code>. We could do this with a <code>setCreatedAt</code> setter. But, if we have lots of date types, this will quickly get repetitive. To make this easy, $.Model lets you define the type of an attribute and a converter function for those types. Set the type of attributes on the static <code>attributes</code> object and converter methods on the static <code>convert</code> object. $.Model('Task',{ attributes : { createdAt : 'date' }, convert : { date : function(date){ return typeof date == 'number' ? new Date(date) : date; } } },{}); Task now converts createdAt to a Date type. To list the year of each task, write: Task.findAll({}, function(tasks){ $.each(tasks, function(){ console.log( "Year = "+this.createdAt.fullYear() ) }) }); ### CRUD Events Model publishes events when an instance has been created, updated, or destroyed. You can listen to these events globally on the Model or on an individual model instance. Use <code>MODEL.bind(EVENT, callback( ev, instance ) )</code> to listen for created, updated, or destroyed events. -
JupiterJS revised this gist
May 26, 2011 . 1 changed file with 50 additions and 123 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -274,21 +274,46 @@ Now the nextPrev plugin can set offset with reckless abandon: paginate.attr('offset', paginate.offset-paginate.limit ); }); ### Defaults We can add default values to Paginate instances by setting the static defaults property object that contains the default attributes. When a new paginate instance is created, if no value is provided, it will use the default value. $.Model('Paginate',{ defaults : { count: Infinity, offset: 0, limit: 100 } },{ setCount : function(newCount, success, error){ ... }, setOffset : function(newOffset, success, error){ ... } }) var paginate = new Paginate(); assertEqual(paginate.limit, 100); This is getting sexu, but the Paginate model can make it event easier to move to the next and previous page and know if it's possible by adding helper methods. ### Helper methods Helper methods are prototype methods that help set or get useful data on model instances. The following, completed, Paginate model includes a <code>next</code> and <code>prev</code> method that will move to the next and previous page if possible. It also provides a <code>canNext</code> and <code>canPrev</code> method that returns if the instance can move to the next page or not. $.Model('Paginate',{ defaults : { count: Infinity, offset: 0, limit: 100 } },{ setCount : function( newCount ){ return Math.max(0, newCount ); }, setOffset : function( newOffset ){ return Math.max( 0 , Math.min(newOffset, this.count ) ) }, next : function(){ this.attr('offset', this.offset+this.limit); }, prev : function(){ this.attr('offset', this.offset - this.limit ) @@ -297,11 +322,11 @@ Helper methods are prototype methods that help set or get useful data on model i return this.offset > this.count - this.limit }, canPrev : function(){ return this.offset > 0 } }) Thus jQuery widget becomes much more refined: $.fn.nextPrev = function(paginate){ this.delegate('.next','click', function(){ @@ -467,119 +492,31 @@ The following table details how to use the task model to CRUD tasks. </tr> </table> The <code>Task</code> model has essentially become a contract to our services! ### CRUD Events Model publishes events when an instance has been created, updated, or destroyed. You can listen to these events globally on the Model or on an individual model instance. Use <code>MODEL.bind(EVENT, callback( ev, instance ) )</code> to listen for created, updated, or destroyed events. Lets say we wanted to know when a task is created and add it to the page. And after it's been added to the page, we'll listen for updates on that task to make sure we are showing its name correctly. We can do that like: Task.bind('created', function(ev, task){ var el = $('<li>').html(todo.name); el.appendTo($('#todos')); task.bind('updated', function(){ el.html(this.name) }).bind('destroyed', function(){ el.remove() }) }) ### Type Conversion ### Events @@ -590,17 +527,7 @@ $.Model produces two types of events: - OpenAjax.hub events - jQuery events Each has advantages and disadvantages for particular situations. For now we'll deal with jQuery events. ### Getter / Setters ?
NewerOlder