javascript - Put a Backbone.js View,Model,Router... inside an object with _.extend() -


i'm building application , i'm using backbone.js. because i'm quiet new backbone, have read there documentation, looked , tested multiple apps , read backbone patterns.

now i'm starting understand whole backbone principal, have little problem something. have global object (example: app) , put views, models,... inside (example: app.views.listview). problem need extend these object, because there default values inside it.

so when started, had this

app.views.listview = backbone.view.extend({}); 

but want this

_.extend(app.views.listview, backbone.view.extend({}); 

this doesn't work want to. app.views.listview exists (it's defined in config file see file object structure , extending something). problem can't make instance of view (ex new app.views.listview();)

examples:

the object created in config

app:{  views:{   listview:{    myvar: "hello"    deeperlist: {    }   }  } } 

if see object app.views.listview contains variable value hello , object deeperlist. inside object want add view without overwriting myvar , deeperlist. when use app.views.listview = backbone.view.extend({}); overwrite it, there way avoid this. using extend...

is had problem of know how solve it?

big thanks!

so object want:

app = {     views: {         listview: {             myvar: "hello"             deeperlist: {}         }     } } 

just jquery extend first parameter set true , you'll deep extend/merge namespace object/namespace

(function(app, listview, undefined){     $.extend(true, listview, {         'deeperlist': backbone.view.extend({          })     }); })(window.app, app.views.listview); 

for more information javascript namespacing visit following interesting blog post: http://addyosmani.com/blog/essential-js-namespacing/

if don't use jquery can use extend function mentioned in blogpost.

function extend(destination, source) {     var tostring = object.prototype.tostring,         objtest = tostring.call({});     (var property in source) {         if (source[property] && objtest == tostring.call(source[property])) {             destination[property] = destination[property] || {};             extend(destination[property], source[property]);         } else {             destination[property] = source[property];         }     }     return destination; }; 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -