html - How to call a module again in Javascript/Backbone which is already loaded -


i new javascript/backbonejs/requirejs. in order render front end have made 1 controller, 1 model , 1 view. also, have 1 dropdown in html file.

so have done till in html file @ end have

 require(['common'],function()     {       require(['jquery','fastclick','nprogress','charts','underscore','spinner'],function()       {         require(['firstdashboardcontroller']);       });     }); 

so loading "firstdashboardcontroller" , controller loads modules accordingly , displays data in front end. works fine.

now have dropdown in front end. when select dropdown, per id selected want retrieve data. need call "firstdashboardcontroller" again gets rendered per new id have got.

so suppose do? need un-require "firstdashboardcontroller" , require again passing new id. because controller loaded via require beacuse loaded in html file mentioned above. need load again per new id selected via dropdown. how go it? pleas me. if code snippet required can put that.

code snippet:

my controller:

define(['backbone', 'firstsubviewmodel','dropdownviewmodel', 'dropdownmodel'],   function(backbone, firstsubviewmodel,  dropdownviewmodel, dropdownmodel) {        var ch = new dashboardmodel.chart({});         if (localstorage.getitem('p_kt') && localstorage.getitem('p_acid') && localstorage.getitem('p_seid')) {           var data = {             tokenprop: localstorage.getitem('p_kt'),             accountidprop: localstorage.getitem('p_acid'),             sessionidprop: localstorage.getitem('p_seid')           };           $.when(             ch.fetch(data) // getting data model via ajax call in model.js            ).then(function() {             // main graph on dashboard             new firstsubviewmodel({               el: '#chartanchor1',               model: ch             });     });}); 

i somehow need call ch.fetch() again.

you aren't defining controller. have sort of one-time setup method instead of can re-run later. let's go step step.

mylife.js:

define([], function() {   return "a complex series of failures"; }); 

by returning value define's callback, defines anytime require "mylife", provide "a complex series of failures" in callback function. backbone , other amd modules appear inside code blocks. however, runs contents once; , saves result. so, won't work:

incrementer.js:

var x = 0; define([], function() {   x = x + 1;   return x; }); 

(trying require incrementer always give "1".)

what want return function - 1 can re-run anytime.

incrementerv2.js:

define([], function() {   var x = 0;   return function() {     x = x + 1;     return x;   }; }); 

in file, can have this:

require(['incrementerv2'], function(myincr) {   myincr(); // 1   myincr(); // 2 }); 

...and, record, recommend having 1 require statement in given file whenever possible. can add dependencies in first-argument array if load order important.

more commonly, people have 1 module contain self-defined object has multiple functions in it, rather function gave above. returning , using 1 function valid well, depending on use case. variable type works, remember 1 , same variable anytime later need it.

load order

when system retrieves mylife.js or incrementer.js above, there's intermediate step before runs definition function we've defined. @ first argument, array of named dependencies, , figure out if there still dependencies needed before can run function given. example:

a.js: require(['b', 'c'], function(b, c) { b.js: define(['c'], function(c) { c.js: define([], function() { 

a.js requested first, not run because needs b , c. b loads next, ignored because c not loaded. c runs, , return value passed , b. system internally smart, , should never request same file twice or have conflicts if 1 file loads before another. can use imports in java.

also, let's added 'c' in a.js b.js wouldn't crash, because needs loaded first - in case, take out of , should work same.

    a.js: require(['b'], function(b) { 

just did, b automatically load dependencies before executes anything. simple principle refer dependency if it's directly referenced in file (or defines necessary global variables)


Comments