i have normal backbone.collection made of user models. models , collection nothing special.
when clear out backbone collection, calling destroy on each of models, reference in program 1 of destroyed models seems remain intact. question - how can write js program when of models in user collection destroyed, reference in appstate.currentuser (see below) gets set null. need use fancy event or 'functional reactive programming' or should javascript able take care of out-of-the-box? right now, appstate.currentuser variable not "become null" when clear out of models, though appstate.currentuser references 1 of backbone user models (!)
i have simple singleton data structure represents state of app - logged-in user, environment, etc:
define('appstate',function() { var appstate = { currentuser: null, authorized: null, hassession: null, env: null }; return { value: appstate, get: function(val){ return appstate[val]; }, set: function(prop,val){ if(prop in appstate){ console.log('application state :',appstate); if(prop === 'currentuser' && val !== null){ appstate['authorized'] = true; } else{ appstate['authorized'] = false; } appstate[prop] = val; console.log('application state changed :',appstate); } else{ throw new error('no appstate property matched.'); } } } }); when user logs in, information gets matched set of registered users so:
collections.users.fetch().done(function () { (var = 0; < collections.users.models.length; i++) { if (user.username === collections.users.models[i].get('username')) { appstate.set('currentuser', collections.users.models[i]); break; } } if (appstate.get('currentuser') == null) { throw new error('null or undefined currentuser'); } else { backbone.events.trigger('bootrouter', 'home'); } }); am doing wrong, or javascript not allow me want out of box? i'd rather not have handle backbone.collection 'destroy' event, maybe there's nothing else can do.
so yes, rather verbose , boilerplate way handle listen model 'destroy' event , set other reference null, requires lot more oversight , more error-prone better solution.
Comments
Post a Comment