更新:我正在用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
答案 0 :(得分:1)
您需要收听有关视图的集合上的reset
事件。
然后使用fetch发送请求。
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)
}
});
为了减少混淆,返回Backbone View,Model或Collection,而不是模块中的新实例。
因此,在定义模块时,您可以创建一个新实例。
// 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 scheduleView;
// Remove the returning of new module here
});
在您要将视图添加为依赖项的模块中
// 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 :
})
<强>更新强>
<强>模型强>
// Filename: models/schedule
define([
'underscore',
'backbone',
'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://localhost:8888/client/i/schedule",
defaults: {
feedback: 'N/A'
},
initialize: function () {
console.log('schedule model loaded successfully');
}
});
return ScheduleModel;
});
<强>集合强>
// Filename: collections/schedule
define([
'jquery',
'underscore',
'backbone',
'models/schedule'], function ($, _, Backbone, ScheduleModel) {
var ScheduleCollection = Backbone.Collection.extend({
model: ScheduleModel,
url: "http://localhost:8888/client/i/schedule",
initialize: function () {
console.log('schedule collections loaded successfully');
}
});
return ScheduleCollection;
});
查看强>
// 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 () {
// 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)
}
});
});
在其他一些模块中,
您需要要呈现的视图
<强>路由器强>
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
}
});