【发布时间】:2014-03-06 13:38:18
【问题描述】:
我正在从服务器接收 JSON,看起来像
{
"accountType": ["Full","Trial"],
"states": [
{"state":"AL","stateDescription":"Alabama","featured":"A1"},
{"state":"AK","stateDescription":"Alaska","featured":"B1"}
],
"dates":[
{"dateDescription":"Jan","month":1,"year":2008},
{"dateDescription":"Feb","month":2,"year":2008}
]
}
在我正在做的主干文件中:
define([ 'backbone', 'underscore', 'vent', ], function (Backbone, _, vent) {
'use strict';
return Backbone.Model.extend({
url: {},
loaded: false,
defaults: {
accountType: [],
states: [],
dates: [],
},
initialize: function () {
this.on('change', this.change);
var that = this;
$.getJSON("webapp/jsondata", function (data) {
that.set({
states: data.states.state,
});
});
$.getJSON("webapp/jsondata", function (data) {
that.set({
dates: data.dates.dateDescription,
});
});
$.getJSON("webapp/jsondata", function (data) {
that.set({
accountType: data.accountType,
});
});
},
});
});
所以每个$.getJSON 都应该获取相关数据并填充主干模型默认值。
目前只有account 类型有效。我不明白为什么这会起作用而其他人不会,因为它是相同的代码。唯一的区别在于 JSON 数据,accountType 有 2 条数据。 States 有 3 个,我只想退回其中的一个 (state)。
所以我想我的问题是在 $.getJSON 代码中指定要检索的数据,但在线很多小时都没有找到答案。
【问题讨论】:
标签: javascript jquery json backbone.js marionette