Sachin
Sachin

Reputation: 2361

createRecord is adding blank record in model

I have posted my code here.

On click of add button,i am trying to create new record in model but here it is adding blank record.

    savecontact: function(){
    App.Person.createRecord({   
          fname: this.get('firstName'),
          lname: this.get('lastName'),
          contactype: 1
        });
        this.get('store').commit(); 
},

Can anyone tell me why createRecord is adding blank record?

Upvotes: 0

Views: 789

Answers (2)

Marcio Junior
Marcio Junior

Reputation: 19128

I have found some problems in your sample:

1 - Your model doesn't have a fname and lname property. Just firstName and lastName.

2 - Your modal is bound to currentContact and when you show the modal, you don't suply the currentContact.

Just create a empty contact and leave the bindings set the values.

showmodal: function(){
    this.set('currentContact', App.Person.createRecord());
    $('#modal').modal();
}

The 1 and 2, is why your data is blank.

3 - You forget to rollback the transaction if the user exit from modal, without saving.

I have done this using:

$(document).delegate('.modal', 'hidden', function() { 
    controller.get('currentContact.transaction').rollback();
}); 

Here is the full result http://jsfiddle.net/marciojunior/GYbeT/

Upvotes: 2

Mike Grassotti
Mike Grassotti

Reputation: 19050

There were a few problems. First, all three of the propertyNames were incorrect. Should be firstName, lastName and contacttype instead of fname, lname and contactype. Second, this.get('firstName') should be this.get('currentContact.firstName') and third you need to initialize currentContact to an empty object when showing the modal. With those changes, the create new record modal is no longer adding blank record.

showmodal: function(){
    this.set('currentContact', {});
    $('#modal').modal();
},
savecontact: function(){
    App.Person.createRecord({
      firstName: this.get('currentContact.firstName'),
      lastName: this.get('currentContact.lastName'),
      contacttype: 1
    });
    this.get('store').commit(); 
},

Updated jsFiddle is here: http://jsfiddle.net/GYbeT/21/

Upvotes: 2

Related Questions