javascript - marionette controller object, create dynamic prototype methods? -
i using marionette app has many pagess. controller/router method repetitive, in each method view instantiated , appregion shows view.
i create methods inside loop.
var controller = marionette.controller.extend({}); for(i=0;i<10;i++) { // build dynamic controller methods }
i have been learning object prototypes , think can like.
var pages = [{'pagebuttons': buttons},{'pagelogin': login}]; for(var page in pages) { for(var method in pages[page]) { console.log(method) // write dynamic method here? } }
my question above block, how finish it, can add new methods in loop? there way simplify repetitive task of adding same code on , over?
i think you're totally on right track. think, there's 2 problems here. outlined first, , second, create , provide routes dynamically router
.
setting controller
view methods , router
i thinking modify reference pages
object this:
var pages = [ {'pagebuttons': buttons, route: 'buttons'}, {'pagelogin': login, route: 'login'} ];
next declare controller , add dynamic methods
var controller = marionette.controller.extend({}), router = marionette.approuter.extend({}); for(var page in pages) { for(var method in pages[page]) { // each method have pointer in // controller.pagemethod, e.g. controller.pagebuttonsbuttons controller[page + method] = pages[page][method]; // , set router // e.g. url of http://appdomain#pagesbutton map // method name 'pagebuttonsbuttons' router[page] = page + method; } }
of course, sky's limit extent customize "method factory". more properties add in reference pages
object more specific factory can be.
i want note here we're not adding methods controller
's prototype, sake of simplicity. , think can away here because commonly controller
s instantiated once , don't need productivity gain of extending prototype. however, there's not functional difference if do.
Comments
Post a Comment