emberjs指南对嵌套模型进行了以下说明:
当您的数据结构中嵌入的数据不使用或不需要 id 时,您必须指定 belongsTo 关系包含在 hasMany 关系中。
为此,您需要扩展应用程序使用的适配器来加载具有嵌入式结构的数据。
App.Comment = DS.Model.extend({});
App.Post = DS.Model.extend({
comments: DS.hasMany('comment')
});
App.Adapter.map('post', {
comments: { embedded: 'always' }
});
我正在使用 restAdapter 所以我认为这样做是正确的:
App.Adapter.map('checkData', { //'App.CheckData' isn't working either
items: { embedded: 'always' }
});
但后来我收到以下错误:(tldr; DS.RESTAdapter 没有方法'map')
Uncaught TypeError: Object function () {
if (!wasApplied) {
Class.proto(); // prepare prototype...
}
o_defineProperty(this, GUID_KEY, undefinedDescriptor);
o_defineProperty(this, '_super', undefinedDescriptor);
var m = meta(this), proto = m.proto;
m.proto = this;
if (initMixins) {
// capture locally so we can clear the closed over variable
var mixins = initMixins;
initMixins = null;
this.reopen.apply(this, mixins);
}
if (initProperties) {
// capture locally so we can clear the closed over variable
var props = initProperties;
initProperties = null;
var concatenatedProperties = this.concatenatedProperties;
for (var i = 0, l = props.length; i < l; i++) {
var properties = props[i];
Ember.assert("Ember.Object.create no longer supports mixing in other definitions, use createWithMixins instead.", !(properties instanceof Ember.Mixin));
for (var keyName in properties) {
if (!properties.hasOwnProperty(keyName)) { continue; }
var value = properties[keyName],
IS_BINDING = Ember.IS_BINDING;
if (IS_BINDING.test(keyName)) {
var bindings = m.bindings;
if (!bindings) {
bindings = m.bindings = {};
} else if (!m.hasOwnProperty('bindings')) {
bindings = m.bindings = o_create(m.bindings);
}
bindings[keyName] = value;
}
var desc = m.descs[keyName];
Ember.assert("Ember.Object.create no longer supports defining computed properties.", !(value instanceof Ember.ComputedProperty));
Ember.assert("Ember.Object.create no longer supports defining methods that call _super.", !(typeof value === 'function' && value.toString().indexOf('._super') !== -1));
Ember.assert("`actions` must be provided at extend time, not at create time, when Ember.ActionHandler is used (i.e. views, controllers & routes).", !((keyName === 'actions') && Ember.ActionHandler.detect(this)));
if (concatenatedProperties && indexOf(concatenatedProperties, keyName) >= 0) {
var baseValue = this[keyName];
if (baseValue) {
if ('function' === typeof baseValue.concat) {
value = baseValue.concat(value);
} else {
value = Ember.makeArray(baseValue).concat(value);
}
} else {
value = Ember.makeArray(value);
}
}
if (desc) {
desc.set(this, keyName, value);
} else {
if (typeof this.setUnknownProperty === 'function' && !(keyName in this)) {
this.setUnknownProperty(keyName, value);
} else if (MANDATORY_SETTER) {
Ember.defineProperty(this, keyName, null, value); // setup mandatory setter
} else {
this[keyName] = value;
}
}
}
}
}
finishPartial(this, m);
this.init.apply(this, arguments);
m.proto = proto;
finishChains(this);
sendEvent(this, "init");
} has no method 'map' app.js:96
(anonymous function)
所以我检查了在 RestAdapter 上调用 map 函数是否正确。但在不同的页面上,我发现:
App.Person = DS.Model.extend({
lastName: DS.attr('string')
});
DS.RESTAdapter.map('App.Person', {
lastName: { key: 'lastNameOfPerson' }
});
文档有错吗?
PS:
DEBUG: Ember.VERSION : 1.0.0 ember-1.0.0.js:394
DEBUG: Handlebars.VERSION : 1.0.0 ember-1.0.0.js:394
DEBUG: jQuery.VERSION : 1.9.1