Cross-posted from discuss.emberjs.com
我正在尝试保存模型,然后在成功保存后保存一些与初始模型相关的其他模型。这些是在标准输入字段中我不能/不知道如何绑定到ED所以我只是使用标准的jQuery选择器来查找输入并保存模型。所有模型都正常保存,但Ember.RSVP.all
由于REJECT
错误而导致Array Methods must be provided an Array
回调。我不确定我应该在哪里提供阵列...
我一直在检查各种元素,var promises
是一系列承诺,如预期的那样。我唯一能想到的是传递给我的resolve回调的results
值应该是一个数组?如果是这种情况,我不知道如何将其作为一个数组,而不仅仅是传入Ember.A()
或[]
这是我控制器中的相关代码。
submit: function () {
var self = this;
var division = this.get('model');
var isNew = division.get('isNew');
if(isNew) {
division.setProperties({
vendor: self.get('parentVendor'),
vendorCategory: self.get('selectedCategory'),
isActive: true,
createdBy: 1,
createdDate: moment().format()
});
} else {
division.setProperties({
vendorCategory: self.get('selectedCategory'),
modifiedBy: 1,
modifiedDate: moment().format()
});
}
division.validate().then(function () {
if (division.get('isValid')) {
division.save().then(function(result) {
/*
this bit of hackishness loops through each of the attribute input fields, creates a new DS.model for that attribute,
saves the model, then returns the resulting promise to the promises array so they can be processed by RSVP.all to
ensure that all the attributes save properly.
*/
// map the promises array
var promises = $('input.attribute').map(function () {
var elem = $(this)[0];
var vendorTypeAttrId = $(elem).attr('data-attribute-id'),
attrValue = $(elem).val();
// create the model
var model = self.store.createRecord('vendor-division-attribute', {
division: result,
vendorTypeAttribute: self.get('categoryAttributes').findBy('id', vendorTypeAttrId),
value: attrValue,
createdBy: 1,
createdDate: moment().format()
});
// return the save promise
var p = model.save();
return p;
});
// Make sure all promises are resolved before continuing
Ember.RSVP.all(promises).then(function (results) {
// it worked! keep calm and move along
self.send('closeModal');
var msg = isNew ? division.get('name') + ' Created!' : 'Changes saved!';
Bootstrap.NM.push(msg, 'success');
}).catch(function(reason) {
// something went wrong - display the error
if (typeof reason.error !== 'undefined') {
// DSP error
if (Ember.keys(reason.error).length > 0) {
error = reason.error[0].message;
} else {
error = 'An error occured while saving your changes';
}
} else if (typeof reason.message !== 'undefined') {
error = reason.message;
} else {
error = reason;
}
console.log(error);
Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
});
}, function(reason) {
var error;
if (typeof reason.error !== 'undefined') {
// DSP error
if (Ember.keys(reason.error).length > 0) {
error = reason.error[0].message;
} else {
error = 'An error occured while saving your changes';
}
} else if (typeof reason.message !== 'undefined') {
error = reason.message;
} else {
error = reason;
}
console.log(error);
Bootstrap.NM.push('Error saving Vendor Division: ' + error, 'danger');
});
}
});
}
答案 0 :(得分:1)
您的promises
数组不是一个真正的数组。 jQuery返回一个类似数组的对象,而不是一个真正的数组。尝试在您的控制台中运行此代码,您将看到:
var a = $('a').map(function() { return 1; });
console.log(Array.isArray(a));
console.log(a instanceof Array);
幸运的是,jQuery提供了一个很好的makeArray
函数来解决这个问题。在您声明promises
之后,但在致电Ember.RSVP.all
之前,请插入此行:
promises = $.makeArray(promises);