Javascript 为什么我的主干数据集没有引入数据?
我没有收到任何错误,但由于某种原因,我的数据没有加载到我的视图中。我用的是Requirejs和木偶。我盯着它看了这么长时间,它可能就在我面前,我只是有一种隧道视觉,看不见它。这是我的档案: 这是我的main.js:Javascript 为什么我的主干数据集没有引入数据?,javascript,backbone.js,marionette,Javascript,Backbone.js,Marionette,我没有收到任何错误,但由于某种原因,我的数据没有加载到我的视图中。我用的是Requirejs和木偶。我盯着它看了这么长时间,它可能就在我面前,我只是有一种隧道视觉,看不见它。这是我的档案: 这是我的main.js: define([ "jquery", "underscore", "backbone", "marionette", "app/views/Listings", "app/coll
define([
"jquery",
"underscore",
"backbone",
"marionette",
"app/views/Listings",
"app/collections/ListingsCollection"], function($, _, Backbone, Marionette, Listings, ListingsCollection){
"use strict";
var App = new Backbone.Marionette.Application();
App.addRegions({
listingRegion: "#listingsView"
});
var listingsCollection = new ListingsCollection();
listingsCollection.fetch().done(function () {
var listingsView = new Listings({collection: listingsCollection});
App.listingRegion.show(listingsView);
});
App.start();
Backbone.history.start();
});
这是我的ListingsItemView.js
define(['marionette', 'tpl!app/views/templates/Listings'],
function (Marionette, template) {
"use strict";
var ListingsItemView = Marionette.ItemView.extend({
template: template()
});
return ListingsItemView;
});
以下是我的ListingsCollectionView.js:
(注意:initialize方法下的这个console.log不会显示在控制台中。)
这是我的模板,数据应该在这里加载:
<div>
<h1>LISTINGS</h1>
<ul>
</ul>
</div>
最后,这里是我的ListingsCollection.js:
define(['backbone', 'marionette', 'app/models/ListingsModel'], function (Backbone, Marionette, ListingsModel) {
'user strict';
var ListingsCollection = Backbone.Collection.extend({
model: ListingsModel,
url: 'http://localhost:1337/post'
});
return ListingsCollection;
});
注意:运行此应用程序时,我在控制台中没有收到任何错误。我只是没有加载任何数据。URLhttp://localhost:1337/post
通过邮递员Chrome分机成功发送此数据:
[
{
"userID": "54e6a5de61505403004698d8",
"title": "Granite counter tops",
"description": "I'm looking for granite counter tops for my kitchen. Willing to trade my skill set.",
"email": "email@gmail.com",
"createdAt": "2015-02-20T03:12:46.349Z",
"updatedAt": "2015-02-20T03:12:46.349Z",
"id": "54e6a62e61505403004698d9"
},
{
"userID": "54e6b0236430c203000ecb86",
"title": "Just a Yahoo looking for a lawn mower",
"description": "need it now",
"email": "email@yahoo.com",
"createdAt": "2015-02-20T03:55:37.705Z",
"updatedAt": "2015-02-20T03:55:37.705Z",
"id": "54e6b0396430c203000ecb87"
},
{
"createdAt": "2015-03-03T05:03:39.176Z",
"updatedAt": "2015-03-03T05:03:39.176Z",
"id": "54f540abdeddf07904a95cab"
},
{
"createdAt": "2015-03-03T05:04:11.428Z",
"updatedAt": "2015-03-03T05:04:11.428Z",
"id": "54f540cbdeddf07904a95cac"
},
{
"userID": "54e6a5d261505403004698d7",
"title": "",
"description": "",
"email": "email@yahoo.com",
"createdAt": "2015-03-03T16:08:06.843Z",
"updatedAt": "2015-03-03T16:08:06.843Z",
"id": "54f5dc660676aa030096bf30"
},
{
"userID": "54e6a5d261505403004698d7",
"title": "My title",
"description": "Descrip",
"email": "email@gmail.com",
"createdAt": "2015-03-03T16:08:21.464Z",
"updatedAt": "2015-03-03T16:08:21.465Z",
"id": "54f5dc750676aa030096bf31"
}
]
在main.js中,您定义了“app/views/Listings”,但根据您的文件名,它应该是“app/views/listingscolectionview”。问题似乎是main.js-我认为最好将获取逻辑移到collection视图,并添加到初始值设定项
this.collection.on('sync',this.render,this)
,由于main.js分担控制器的职责,所以您可能只保留视图、模型的实例并将其连接起来。另外,在应用程序启动时运行初始操作也是一个很好的做法,因此在初始化模型的地方包装代码,并在('start')回调中查看。最后一点是tpl代码>-它是否返回函数?经常文本代码>正在使用,它返回模板
define(['backbone', 'marionette', 'app/models/ListingsModel'], function (Backbone, Marionette, ListingsModel) {
'user strict';
var ListingsCollection = Backbone.Collection.extend({
model: ListingsModel,
url: 'http://localhost:1337/post'
});
return ListingsCollection;
});
[
{
"userID": "54e6a5de61505403004698d8",
"title": "Granite counter tops",
"description": "I'm looking for granite counter tops for my kitchen. Willing to trade my skill set.",
"email": "email@gmail.com",
"createdAt": "2015-02-20T03:12:46.349Z",
"updatedAt": "2015-02-20T03:12:46.349Z",
"id": "54e6a62e61505403004698d9"
},
{
"userID": "54e6b0236430c203000ecb86",
"title": "Just a Yahoo looking for a lawn mower",
"description": "need it now",
"email": "email@yahoo.com",
"createdAt": "2015-02-20T03:55:37.705Z",
"updatedAt": "2015-02-20T03:55:37.705Z",
"id": "54e6b0396430c203000ecb87"
},
{
"createdAt": "2015-03-03T05:03:39.176Z",
"updatedAt": "2015-03-03T05:03:39.176Z",
"id": "54f540abdeddf07904a95cab"
},
{
"createdAt": "2015-03-03T05:04:11.428Z",
"updatedAt": "2015-03-03T05:04:11.428Z",
"id": "54f540cbdeddf07904a95cac"
},
{
"userID": "54e6a5d261505403004698d7",
"title": "",
"description": "",
"email": "email@yahoo.com",
"createdAt": "2015-03-03T16:08:06.843Z",
"updatedAt": "2015-03-03T16:08:06.843Z",
"id": "54f5dc660676aa030096bf30"
},
{
"userID": "54e6a5d261505403004698d7",
"title": "My title",
"description": "Descrip",
"email": "email@gmail.com",
"createdAt": "2015-03-03T16:08:21.464Z",
"updatedAt": "2015-03-03T16:08:21.465Z",
"id": "54f5dc750676aa030096bf31"
}
]