javascript - backbone.js - controller properties from a view -


i have controller property called authenticated defaults false. however, in login view need able set true. also, in logout view need able set false. how can expose property within view?

var controller = backbone.controller.extend({     ...     authenticated: false,      login: function() {         if(this.authenticated)         {             location.hash = '!/dashboard';         } else {             new loginview();         }     },      logout: function() {         $.post('/admin/logout', {},         function(resp){         }, "json");          this.authenticated = false;         location.hash = '!/login';     }     ... }); 

your controller correctly doing login , logout functionality. need have view fire backbone.js events , have controller registered receive those.

somewhere in controller, need like:

var loginview = new loginview(...);  // params needed loginview.bind("login_view:login", this.login); loginview.bind("login_view:logout", this.logout); loginview.render(); 

also, need assure controller set handle events, needed in initialize function:

_.extend(this, backbone.events); _.bindall(this, "login", "logout"); 

your view need event code, sure add _.extend(...) call initialize.

in view appropriate, need:

this.trigger("login_view:login"); 

and

this.trigger("login_view:logout"); 

as final note, want controller login , logout server calls. need view event , potentially populated model or data otherwise. data passed parameter in trigger statement(s) , received argument in login/logout functions. have not included in code, however.

you want view manage dom , bubble application events controller. controller can mediate server , manage necessary views.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -