Saving data captured from form rendered in a modal component

Ember - v1.7.0
Ember Data - v1.0.0-beta.10


I created a modal component using zurb foundation 5 CSS framework reveal features, though all works well, am unable to save data captured from the form in controller save action.

Controller which handles on save button execution

App.PersonModalController = Ember.ObjectController.extend({
    actions: {
        close: function() {
            return this.send( 'closeModal' );
        },

        save:function() {
            this.get('model').save();
        }
    }
});

The issue am facing is that the this.get('model').save() is not working and data is not been posted to restful backend.

Am not sure exactly how to go about storing the data captured from the form, when I console.log( this.get('model') ); it appears to be a proper model object with all the bells and whistles.

I tried obtaining the store to add model to it but that doesn't work too.

A. Addendum

After searching around I came across a number of Stack Overflow questions relating to this.get('model').save() it appears it doesn't quite work as expect, perhaps based on context.

difference-between-model-save-versus-model-getstore-commit
ember-js-how-to-save-a-model
save-record-of-model-is-not-working-in-ember-data-1-0-0-beta-3

When I change code to the following:

App.PersonModalController = Ember.ObjectController.extend({
    actions: {
        close: function() {
            return this.send( 'closeModal' );
        },

        save:function() {
            var person = this.store.createRecord('person',{firstName:firstName,lastName:lastName});
            person.save();
        }
    }
});

It POSTs data correctly to back-end and saves, I however believe there must be a better way, cause if you have a form with say 50 fields, you won't want to manually set each attribute.

After careful inspection, though posting occurs, the data posted is empty.


I would try to continue your save method as in Bart's answer to the ember-js-how-to-save-a-model question.

person.save().then(function() {
  // SUCCESS
}, function() {
  // FAILURE
});

and in those methods I would console.log() the results.

I would imagine it has something to do with the promise aspect of the save functionality.

链接地址: http://www.djcxy.com/p/64078.html

上一篇: 如何处理部分嵌入的记录?

下一篇: 保存从模态组件中呈现的表单中捕获的数据