2013-08-25 56 views
4

我是backbone.js的新用戶,測試如何解決這個問題,最近幾天我測試瞭如何使用路徑來更改視圖數據通過集合。當創建視圖的實例時,Backbone.js不是構造函數錯誤

在目前的情況下,我被套牢的一個問題是,當我試圖在router.js創建ScheduleView的實例控制檯登錄此錯誤消息:

TypeError: ScheduleView is not a constructor 

以下三個頁面的代碼時間表模塊{視圖,收集,模型} +的router.js

路由器的

// Filename: router.js 
define([ 
    'jquery',  
    'underscore', 
    'backbone', 
    'app/views/schedule', 
    'app/collections/schedule' 
], function($, _, Backbone, ScheduleView, ScheduleCollection) { 
    var AppRouter = Backbone.Router.extend({ 
     routes: { 
      // Define some URL routes 
      'schedule': 'scheduleRoute', 
      // Default 
      '*actions': 'defaultRoute' 
     }, 
     scheduleRoute: function() { 
      // Create a new instance of the collection 
      // You need to set the url in the collection as well to hit the server 
      var schedulecollection = new ScheduleCollection(); 
      // Pass in the collection as the view expects it 
      console.log(typeof(ScheduleView)); 
      var scheduleview = new ScheduleView({ 
       collection: schedulecollection 
      });    
      // No need of calling render here 
      // as the render is hooked up to the reset event on collection   
     }, 
     defaultRoute: function(actions) {    
      // We have no matching route, lets display the home page 
      //DashboardView.render(); 
     } 
    }); 

    var initialize = function() {     
     var app_router = new AppRouter; 
     Backbone.history.start(); 
    }; 
    return { 
     initialize: initialize 
    }; 
}); 

// Filename: views/schedule 
define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'app/collections/schedule', 
    'text!templates/schedule.html' 
], function ($, _, Backbone, ScheduleCollection, ScheduleTemplate) { 

    var scheduleView = Backbone.View.extend({   
     el: $(".app"), 
     initialize: function() { 
      // Listen to the reset event which would call render 
      this.listenTo(this.collection, 'reset', this.render); 
      // Fetch the collection that will populate the collection 
      // with the response 
      this.collection.fetch(); 
     }, 
     render: function() { 
      //console.log('schedule view loaded successfully'); 
      console.log(this.collection); 
     } 
    });   
}); 

收集

// Filename: collections/schedule 
define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'app/models/schedule' 
], function ($, _, Backbone, ScheduleModel) { 
    var ScheduleCollection = Backbone.Collection.extend({ 
     model: ScheduleModel, 
     url: "http://sam-client:8888/sam-client/i/schedule", 
     initialize: function() { 
      //console.log('schedule collections loaded successfully'); 
     } 
    }); 
    return ScheduleCollection; 
}); 

示範

// Filename: models/schedule 
define([ 
    'underscore', 
    'backbone', 
    'app/config'], function (_, Backbone, Config) { 
    var ScheduleModel = Backbone.Model.extend({ 
     // If you have any 
     //idAttribute : 'someId' 
     // You can leave this as is if you set the idAttribute 
     // which would be apprnded directly to the url 
     urlRoot: "http://sam-client:8888/sam-client/i/schedule", 
     defaults: { 
      feedback: 'N/A' 
     }, 
     initialize: function() { 
      console.log('schedule model loaded successfully'); 
     } 
    }); 
    return ScheduleModel; 

}); 
+1

那麼,是什麼'console.log'? – Bergi

回答

6

看起來你ScheduleView未定義 - 你沒有從模塊輸出任何東西。你似乎已經忘記了線

return scheduleView; 
+1

是的,謝謝:) – ahmedsaber111

+1

我甚至返回視圖,但仍然得到上述錯誤。 –

+0

尋找我的失蹤返回修正了我的「TypeError:*不是構造函數」錯誤 – thehme

1

您定義ScheduleView模型,但你忘了還:

// Filename: views/schedule 
define([ 
    'jquery', 
    'underscore', 
    'backbone', 
    'app/collections/schedule', 
    'text!templates/schedule.html' 
], function ($, _, Backbone, ScheduleCollection, ScheduleTemplate) { 

    var scheduleView = Backbone.View.extend({   
     el: $(".app"), 
     initialize: function() { 
      // Listen to the reset event which would call render 
      this.listenTo(this.collection, 'reset', this.render); 
      // Fetch the collection that will populate the collection 
      // with the response 
      this.collection.fetch(); 
     }, 
     render: function() { 
      //console.log('schedule view loaded successfully'); 
      console.log(this.collection); 
     } 
    }); 
    /****** ADD THIS *****/ 
    return scheduleView;  
}); 
+0

yesssss Bergi說這是問題 – ahmedsaber111

+0

Bergi說這是解決方案...在fatman之前1分鐘。 –

相關問題