1 / 30

MVVM/MVC in Client-side SPA

MVVM/MVC in Client-side SPA. var subTitle = { value: ‘Single page application approaches and usage’, speaker1: ‘ Stanimir Todorov , Product Developer Infragistics ’, speaker2: ‘Konstantin Dinev , Software Engineer Infragistics ’ } ;. Contents . Past vs. present Usage Approaches

bono
Download Presentation

MVVM/MVC in Client-side SPA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MVVM/MVC in Client-side SPA varsubTitle = { value: ‘Single page application approaches and usage’, speaker1: ‘StanimirTodorov, Product Developer Infragistics’, speaker2: ‘Konstantin Dinev, Software Engineer Infragistics’ };

  2. Contents • Past vs. present • Usage • Approaches • Frameworks and libraries • Knockout.js • Backbone.js • Hands on

  3. Past vs. present • Past • Static HTML • Server rendered HTML • Present • JavaScript

  4. Example

  5. Example

  6. Example

  7. Usage Improve User Experience

  8. Approaches • AJAX • Server API • JSON / XML / AJAX

  9. Frameworks and libraries • Libraries • Knockout.js • Frameworks • Backbone.js • Angular.js

  10. MVVM • Handles single or multiple ViewModels • Observer pattern • DOM elements bind to the model • Built-in templating

  11. Example // Here's my data model varViewModel = function(first, last) { this.firstName = ko.observable(first); this.lastName = ko.observable(last); this.fullName = ko.computed(function() {         // Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.         return this.firstName() + " " + this.lastName();     }, this); }; ko.applyBindings(new ViewModel("Planet", "Earth")); // This makes Knockout get to work

  12. Hands on Knockout.js

  13. MVC

  14. Overview • Models • Views • Collections • RESTful Persistence • Events • Routers • Dependencies

  15. Model Example var Task = Backbone.Model.extend({});   var task = new Task({title: “Fix Bugs”});   var title = task.get(“title");

  16. Model Concepts • Default values • Getters & Setters • Listening for changes to your model • Validation

  17. View Example • Template <script type="text/template" id=“task-template">       <div class="view">           <p>Title: <input type="text" value="<%=title%>"/></p> </div> </script> • Rendering Container <div id="container">Your task goes here.</div> • Define the script varTaskView= Backbone.View.extend({ el: '#container', model: task, template: _.template($("#task-template").html()), initialize: function(){this.render();}, render: function(){this.$el.html(this.template(this.model.toJSON()));} }); // initialize view new TaskView();

  18. Collections Example varTasksCollection= Backbone.Collection.extend({ model: Task }); varmyTask= new Task({title:'Read the whole book', id: 2}); var tasks = new TasksCollection([myTask]);

  19. Collections Concepts • Adding / Removing Models • Retrieving Models • Listening for events • Resetting / Refreshing • Underscore utility functions • Chainable API

  20. RESTful Persistence • Fetching models • Saving models to the server • Deleting models from the server

  21. Events Example 1 / 2 vartask = {}; // Mixin _.extend(task, Backbone.Events); // Add a custom event task.on(‘completed’, function(msg){…}); // Trigger the custom event task.trigger(‘completed‘, 'our event'); // Removes event bound to the object obj.off(‘completed’)

  22. Events Example 2 / 2 var a = _.extend({}, Backbone.Events); var b = _.extend({}, Backbone.Events); var c = _.extend({}, Backbone.Events); // add listeners to A for events on B and C a.listenTo(b, 'anything', function(event){ console.log("anything happened"); }); a.listenTo(c, 'everything', function(event){ console.log("everything happened"); }); // trigger an event b.trigger('anything'); // logs: anything happened // stop listening a.stopListening(); // A does not receive these events b.trigger('anything'); c.trigger('everything');

  23. Router Example varTaskRouter= Backbone.Router.extend({ /* define the route and function maps for this router */ routes: { "about" : "showAbout", "search/:query" : "searchTasks", "search/:query/p:page" : "searchTasks" }, showAbout: function(){…}, searchTasks: function(query, page){ varpage_number = page || 1; … } }); varmyTaskRouter= new TaskRouter(); Backbone.history.start();

  24. Dependencies • Underscore || Lo-Dash • Json2.js for RESTful (IE7), history support via Backbone.Router and DOM manipulation with Backbone.View • jQuery || Zepto

  25. Hands on Backbone.js

  26. Resources • https://github.com/thomasdavis/backbonetutorials/blob/gh-pages/videos/beginner/index.html • https://github.com/addyosmani/todomvc/blob/gh-pages/architecture-examples/backbone/js/views/app.js • http://www.infragistics.com/community/blogs/nanil/archive/2013/04/01/exploring-javascript-mv-frameworks-part-1-hello-backbonejs.aspx • http://addyosmani.github.com/backbone-fundamentals/ • http://www.infragistics.com/products/jquery • http://knockoutjs.com/

  27. Expect very soon: SharePoint Saturday! • Saturday, June 8, 2013 • Same familiar format – 1 day filled with sessions focused on SharePoint technologies • Best SharePoint professionals in the region • Registrations will be open next week (15th)! • www.SharePointSaturday.eu

  28. Thanks to our Sponsors: Diamond Sponsor: Platinum Sponsors: Gold Sponsors: Swag Sponsors: Media Partners:

More Related