backbone.js - Cannot read property 'home' of undefined -
i'm new backbone.js , requirejs , i'm trying figure out following situation.
when load app in chrome during "context.onscriptload" initialize function of router called. here i'm using app controller it's still undefined. so, i'm getting error saying:
cannot read property 'home' of undefined
my router defined as:
define(['jquery', 'underscore', 'backbone', 'components/appcontroller'], function ($, _, backbone, appcontroller) { var router = backbone.router.extend({ routes: { '': 'home', 'details/:id': 'details', 'createagent': 'createagent' }, initialize: function () { var routename; (var r in this.routes) { routename = this.routes[r]; this.route(r, routename, $.proxy(appcontroller[routename], appcontroller)); } }, start: function () { backbone.history.start(); } }); return new router(); } );
it seems appcontroller not initialized.
the controller defined as:
define(['jquery', 'underscore', 'backbone'], function ($, _, backbone) { var appcontroller = { currentview: null, home: function () { var self = this; require(['views/homeview'], function (homeview) { var view = new homeview(); self.renderview.call(self, view); }); }, details: function (id) { var self = this; require(['views/detailsview'], function (detailsview) { var agent = app.agents.get(id); var view = new detailsview({ model: agent }); self.renderview.call(self, view); }); }, createagent: function () { var self = this; require(['views/createview'], function (createview) { var view = new createview(); self.renderview.call(self, view); }); }, renderview: function (view) { this.currentview && this.currentview.remove(); $('#main').html(view.render().el); this.currentview = view; } } });
my app.js is
requirejs.config({ baseurl: 'scripts/libs', paths: { models: '../models', collections: '../collections', views: '../views', routers: '../routers', components: '../components', modaldialog: 'backbone.modaldialog' }, shim: { 'backbone': { deps: ['underscore', 'jquery'], exports: 'backbone' }, 'underscore': { exports: '_' } } }) var app = app || {}; require(['routers/router', 'components/dataservice'], function (router, dataservice) { $(document).ready(function () { dataservice.getdata(); router.start(); }); });
can explain how resolve issue?
your controller must "return appcontroller;"
Comments
Post a Comment