更新:Sushanthの回答で質問を更新しています--、コードが正常に実行されないという問題に直面しました[この引用「最新の更新」とその下の問題の後の質問のコードの最新の更新]
Backbone.js アプリケーションを開発していますが、サーバーからデータを取得することに行き詰まっています。
http://localhost:8888/client/i/schedule
この URL は、必要なデータの json 配列を表します。ここでの問題は、コレクションとモデルからこのデータをビューに読み取らせる方法です。
以下の 3 つのファイルがあります。
最初のものはビュー用です
// Filename: views/schedule
define([
'jquery',
'underscore',
'backbone',
'collections/schedule',
'text!templates/schedule.html'
], function($, _, Backbone, scheduleCollection, scheduleTemplate) {
var scheduleView = Backbone.View.extend({
el: $(".app"),
initialize: function() {},
render: function() {
console.log('schedule view loaded successfully');
}
});
return new scheduleView;
});
2枚目はコレクション用
// Filename: collections/schedule
define([
'jquery',
'underscore',
'backbone',
'models/schedule'
], function($, _, Backbone, ScheduleModel){
var ScheduleCollection = Backbone.Collection.extend({
model: ScheduleModel,
initialize: function(){
console.log('schedule collections loaded successfully');
}
});
return new ScheduleCollection;
});
最初のものはモデル用です
// Filename: models/schedule
define([
'underscore',
'backbone',
'config'
], function(_, Backbone, config) {
var ScheduleModel = Backbone.Model.extend({
urlRoot: "http://localhost:8888/client/i/schedule",
defaults: {
feedback: 'N/A'
},
initialize: function(){
console.log('schedule model loaded successfully');
}
});
return ScheduleModel;
});
アップデート
ルーター
var AppRouter = Backbone.Router.extend({
routes: {
// Define some URL routes
'schedule': 'scheduleRoute',
// Default
'*actions': 'defaultRoute'
},
scheduleRoute: function() {
scheduleView.render();
},
)}
最新のアップデート
router.js
define([
'jquery',
'underscore',
'backbone',
'app/views/dashboard',
'app/views/schedule'
], function($, _, Backbone, dashboardView, scheduleView) {
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 schedules = new Schedules();
// Pass in the collection as the view expects it
var scheduleView = new ScheduleView({
collection: schedules
});
// 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
};
});
スケジュール ビュー .js
// Filename: views/schedule
define([
'jquery',
'underscore',
'backbone',
'app/collections/schedule',
'text!templates/schedule.html'
], function ($, _, Backbone, scheduleCollection, scheduleTemplate) {
var scheduleView = Backbone.View.extend({
collection: scheduleCollection,
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);
}
});
return new scheduleView;
});
コレクション
// 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;
});
問題 のコードが期待どおりに実行されず、コンソールに以下のエラーが記録されます
Uncaught TypeError: Cannot read property '_listenerId' of undefined
答えを更新 すると、ScheduleView.js から値を返すことができないということでした