【问题标题】:Ember.RSVP.All error Array Methods must be provided an ArrayEmber.RSVP.All error Array 方法必须提供一个 Array
【发布时间】:2014-12-20 15:32:07
【问题描述】:

Cross-posted from discuss.emberjs.com

我正在尝试保存一个模型,然后在成功保存后保存一些与初始模型相关的其他模型。这些在标准输入字段中,我不能/不知道如何绑定到 ED,所以我只是使用标准 jQuery 选择器来查找输入并保存模型。所有模型都正确保存,但Ember.RSVP.all 由于Array Methods must be provided an Array 错误而触发REJECT 回调。我不确定我应该在哪里提供数组...

一路上我检查了各种元素,var promises 是一系列预期的承诺。我唯一能想到的是传递给我的解析回调的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');
            });
        }
    });
}

【问题讨论】:

    标签: ember.js rsvp.js rsvp-promise


    【解决方案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);
    

    【讨论】:

    • 所以问题实际上出在$('input.attribute').map(function () { ... } 上?在阅读了$.map docs 之后,似乎是这样。感谢您的澄清!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多