I'm trying to learn and make backbone.marionette app and have following 2 doubts/issues:
1)
I'm trying to redirect a user to home page, instead of default login page, if he is already logged in. To check user is already login I have used jquery.cookie inside a model and use that model class on start of marionette application to check where to redirect. The cookie works fine, I get the value from cookie, but my application doesn't routes to home page. I'm doing this on app start event.App.on("start", function(options) {var session = new UserSession(); // UserSession is model containing jquery.cookie part if (session.authenticated()) { console.log("redirecting..."); App.trigger("users:list"); console.log("redirected"); } } In another file I have App.addInitializer( function (options) { new Backbone.Marionette.AppRouter.extend({ appRoutes: { "users": "listUsers" }, controller: MyRouteCtlr } }); App.on("users:list", function(){ console.log("triggered users list"); App.navigate("users"); MyRouteCtlr.listUsers(); }); The console out put is: redirecting... redirected What am I missing here? How I can redirect user to other page than default on page refresh after checking for login in cookie? Btw I'm using Require.js for dependencies.
Update: I have resolved first point, it was due to load ordering in require functions.
2) Another thing I tried to make a base-model and than have all others to extend that model. So for that I created base model in a file as follows:
define([
'backbone'
],
function( Backbone ) {
'use strict';
return Backbone.Model.extend({
initialize: function() {
console.log("BaseModel");
},
defaults: {}
});
});
Then I try to extend it in another file as :
define([
'backbone',
'localstorage',
'base-model'
],
function( Backbone, BaseModel ) {
'use strict';
return BaseModel.extend({
initialize: function() {
console.log("sub model");
},
});
});
But when I run it gives error at line return BaseModel.extend(...
saying undefined is not a function
. If I write the base mode in same file then runs perfectly fine but not with different files. I want to have it in different file so all other models can also extend it. The same thing works for view without any error. What is going wrong here and how can I resolve that?
The BaseModel in extended class prints Backbone.LocalStorage.window.Store {name: undefined, serializer: Object, records: Array[0], save: function, create: function…}
while in main js containing application object prints child {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
.
console.log(BaseModel)
I imagine it is undefined and you haven't required in the right path – DominicBackbone.LocalStorage.window.Store {name: undefined, serializer: Object, records: Array[0], save: function, create: function…}
– Harry JoyApp.on('start', function(){...})
it prints likechild {cid: "c2", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
which actually seems correct. – Harry Joy