【发布时间】:2013-08-25 18:31:29
【问题描述】:
更新:我正在用 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;
});
第二个用于收藏
// 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 中返回一个值
Backbone.js is not a constructor error when create instance of view
【问题讨论】: