javascript - DurandalJS: Keeping data across a session -


i have decided create separate folder called "controllers" ajax requests, models , data arrays, in case need same requests on multiple pages, wouldn't have repeat code.

so example on 'app/page1/index.html' have list of users:

<ul data-bind="foreach: { data: usersarr }">   <li data-bind="text: full_name"></li> </ul> 

i require user controller in page1's viewmodel , load data through it:

define(['jquery', 'knockout', 'controllers/user/index'], function ($, ko, user) {   var page1 = function() {     this.usersarr = user.usersarr;   };    page1.prototype.activate = function() {     user.getusers();   };    return page1; }); 

and user controller:

define(['knockout', './models',], function (ko, model) {   var usercontroller = function() {     this.usersarr = ko.observablearray();   };    // inherit   usercontroller.prototype.model = new model();    usercontroller.prototype.getusers = function () {     // ajax goes here, data comes back,     // put through model , save usersarr();   };    usercontroller.prototype.adduser = function () { ... };   usercontroller.prototype.removeuser = function () { ... };    return new usercontroller(); }); 

notice return controller object. did stays in memory , reuse code on other pages (especially observable array stored data). if create 'page2' example , require same list, users loaded in usersarr observable array.

i have multiple controllers 1 app use throughout session. wanted know if there downsides method, app slow-downs or other problems may encounter. i'd love know if guys have different approach keeping data across session.

thanks!

the best way i've found keep information around in spa create singleton , reference singleton in views.

for instance, modified version of 1 of mine:

define([], function() {     "use strict";     var countrylist: []           return {       countrylist: countrylist,      }; }); 

and require on of spas. countrylist filled in first view[1] , data available others.

[1] in context making wizard, must go through views in order.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -