获取嵌套json主干集合后,我的模型在哪里
Total Backbone js noob,我试图了解我正在使用的返回json的api是否不适合主干(或者不适合任何东西),或者我只是不知道集合和模型在主干中如何工作 我知道集合的fetch正确地返回了json,这可能就是我处理响应的方式。令人困惑的是,我真的不知道正确获取的集合想要什么,以及获取后模型存储在哪里。看起来我想要的模型是作为对象存储在集合的模型属性中的。。。是这样吗 另外,我想作为第二个问题,我的集合似乎或多或少没有解析每个模型,而是只创建了一个包含我的模型集合的模型。我不知道该怎么做,但我希望这是另一种方式,一个我的模型集合。有什么建议吗 让我列出一些代码,然后提供一个json响应示例(我的示例仅包含3个对象)和一个获取集合的控制台日志。非常感谢您的帮助和建议。谢谢 我的引导文件:获取嵌套json主干集合后,我的模型在哪里,json,backbone.js,nested,fetch,backbone.js-collections,Json,Backbone.js,Nested,Fetch,Backbone.js Collections,Total Backbone js noob,我试图了解我正在使用的返回json的api是否不适合主干(或者不适合任何东西),或者我只是不知道集合和模型在主干中如何工作 我知道集合的fetch正确地返回了json,这可能就是我处理响应的方式。令人困惑的是,我真的不知道正确获取的集合想要什么,以及获取后模型存储在哪里。看起来我想要的模型是作为对象存储在集合的模型属性中的。。。是这样吗 另外,我想作为第二个问题,我的集合似乎或多或少没有解析每个模型,而是只创建了一个包含我的模型集合的模型。我不知道该
//statusApp.js
(function($){
var statusGroup = new app.StatusCollection();
var deferred = statusGroup.fetch({
data: {
action: "get_my_status_collection"
}
});
/* wait till fetch is complete */
$.when(deferred).then(function(){
console.log('returned collection', statusGroup);
var statusGroupView = new app.allStatusView({ collection: statusGroup});
$(".service-statuses").html(statusGroupView.render().el);
var statusRouter = new app.Router();
Backbone.history.start();
});
})(jQuery);
下面是接收到的响应示例,显示了它从服务器附带的所有嵌套节点。您将在我的集合中看到,parse函数只返回“objects”节点,而我不需要其余的节点(它可能不适用于这些顶级节点?)
以下是从集合获取返回的json的控制台日志:
statusGroup
d {length: 1, models: (...), _byId: Object, url: (...), trigger: function…}
[object Object]: undefined
get [object Object]: function () {
set [object Object]: function (newval) {
__backboneDebugger__appComponentInfo: window.__backboneAgent.AppComponentInfo
__backboneDebugger__isInstancePatched: true
_byId: Object
initialize: function () {
length: 1
models: Array[1]
0: d
__backboneDebugger__appComponentInfo: window.__backboneAgent.AppComponentInfo
__backboneDebugger__isInstancePatched: true
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
2: Object
comment: ""
description: ""
name: "Tech Portal"
parent: "0"
status: "3"
get 2: function () {
set 2: function (newval) {
80: Object
comment: ""
description: "Exchange Calendar"
name: "Calendar"
parent: "0"
status: "2"
get 80: function () {
set 80: function (newval) {
220: Object
comment: ""
description: ""
name: "Solution Services"
parent: "0"
status: "3"
get 220: function () {
set 220: function (newval) {
watchers: Object
__proto__: Object
get attributes: function () {
set attributes: function (newval) {
changed: Object
cid: (...)
get cid: function () {
set cid: function (newval) {
collection: (...)
get collection: function () {
set collection: function (newval) {
id: (...)
get id: function () {
set id: function (newval) {
initialize: (...)
sync: function (){return b.sync.apply(this,arguments)}
trigger: function (a){if(!this._events)return this;var b=g.call(arguments,1);if(!j(this,"trigger",a,b))return this;var c=this._events[a],d=this._events.all;return c&&k(c,b),d&&k(d,arguments),this}
urlRoot: (...)
get urlRoot: function () {
set urlRoot: function (newval) {
watchers: Object
__proto__: f
get 0: function () {
set 0: function (newval) {
length: 1
pop: function () {
push: function () {
reverse: function () {
shift: function () {
slice: function () {
sort: function () {
unshift: function () {
watchers: Object
__proto__: Array[0]
get models: function () {
set models: function (newval) {
sync: function (){return b.sync.apply(this,arguments)}
trigger: function (a){if(!this._events)return this;var b=g.call(arguments,1);if(!j(this,"trigger",a,b))return this;var c=this._events[a],d=this._events.all;return c&&k(c,b),d&&k(d,arguments),this}
url: (...)
get url: function () {
set url: function (newval) {
watchers: Object
__proto__: f
我的收藏:
// allStatus.js
var app = app || {};
// A group (array) of Status models
app.StatusCollection = Backbone.Collection.extend({
model: app.singleStatus,
url: "/remote/api/status_json",
parse: function(response){
//return only the nested objects that will be our models
return response.component.objects;
}
});
我的收藏视图:
// allStatusView.js
var $ = jQuery.noConflict();
var app = app || {};
app.allStatusView = Backbone.View.extend({
tagName: "ul",
render: function() {
this.collection.each(this.addStatus, this);
return this;
},
addStatus: function(status) {
var statusView = new app.singleStatusView({ model: status });
this.$el.append(statusView.render().el);
}
});
我的单一观点:
// singleStatusView.js
var $ = jQuery.noConflict();
var app = app || {};
app.singleStatusView = Backbone.View.extend({
tagName: "li",
className: "service-status",
template: _.template( $("#statusElement").html() ),
render: function() {
var statusTemplate = this.template(this.model.toJSON());
this.$el.html(statusTemplate);
return this;
}
});
以下是如何将“对象”转换为收藏所需的格式:
app.StatusCollection = Backbone.Collection.extend({
model: app.singleStatus,
url: "/remote/api/status_json",
parse: function (response){
var obj = response.component.objects;
// add the additional properties that won't be in your "models"
// to the collection object directly if you want them
this.status = response.component.status;
this.count = response.component.count;
// convert the "objects" object to an array and return
// the resulting array
return _.map(obj, function (value, key) {
return obj[key];
});
}
});
我将“组件”中的其他属性直接添加到集合对象中,但这并不是必需的-它们仅在需要保留这些属性时才存在。您可以对末尾不属于“组件”的其他属性执行相同的操作。因为您的响应是一个对象,而不是一个集合(列表/数组),所以您需要重写集合的解析方法,以获得集合所需的格式。您实际需要访问数据的哪一部分?感谢响应,我没有意识到集合不能是对象,嗯。我正在覆盖集合的解析(请参见上面的集合代码块),以访问返回响应的“对象”节点。“对象”节点是我考虑模型的每个对象所在的位置。我基本上需要访问这些对象的“name”、“status”、“parent”和“description”值。我将编写一个版本的parse,我认为它可以满足您的需要。完成了,非常感谢!我可能还有其他一些问题,但它现在确实起作用了。还包括额外的属性,我也需要这些。谢谢!:)
app.StatusCollection = Backbone.Collection.extend({
model: app.singleStatus,
url: "/remote/api/status_json",
parse: function (response){
var obj = response.component.objects;
// add the additional properties that won't be in your "models"
// to the collection object directly if you want them
this.status = response.component.status;
this.count = response.component.count;
// convert the "objects" object to an array and return
// the resulting array
return _.map(obj, function (value, key) {
return obj[key];
});
}
});