Sea guy
Sea guy

Reputation: 178

Detect Event When the model is created dynamically in emberjs

I have created some model App.Markets
In here I am going to display "hello world".

App.DashboardController = Ember.ObjectController.extend({
    test: function(){
    var post = App.Markets.createRecord({
       name: "NAME123",
       created: "CREATED123"
    });
    post.one('didCreate', function() {
      console.log("hello world");
    });
    post.save();
}

But I cannot see this message when I create a new record in App.Markets Model.

Upvotes: 1

Views: 84

Answers (2)

intuitivepixel
intuitivepixel

Reputation: 23322

I don't know how your application setup looks like, but have a look at this simple demo that uses the FixtureAdapter for simplicity reasons who show's that it's working.

Let me know if it helps.

Upvotes: 0

Mike Grassotti
Mike Grassotti

Reputation: 19050

The didCreate hook is triggered when adapter has gotten confirmation from your API that the record was saved. If you cannot see this message it's probably because there was an error calling your API.

To see what is going on, setting the model's stateManager.enableLogging property to true. With this in place you will be able to see console.log messages as the model transitions between states

App.DashboardController = Ember.ObjectController.extend({
    test: function(){
    var post = App.Markets.createRecord({
       name: "NAME123",
       created: "CREATED123",
       "stateManager.enableLogging": true
    });
    post.one('didCreate', function() {
      console.log("hello world");
    });
    post.save();
}

Upvotes: 1

Related Questions