samedi 30 mai 2015

Ember not creating an id for new instances

I created a prototype of an Ember app using the Local Storage adapter that I am not trying to port to using the REST adapter with a backend store. In the local storage version, I noticed that Ember creates an id for an new instance of a model prior to the instance being saved (and also even if the instance is never saved). For example, in my local storage app, I can log the id in both places

var gecko = this.store.createRecord('gecko', {
    date: new Date(),
    type: "gecko",                                     
    });
  console.log(gecko.id, "gecko.id before save");
  gecko.save();
  console.log(gecko.id, "gecko.id");

By contrast, in the version that I'm making with the REST adapter for the backend store, the there is NO id being logged, and when I check the data that Ember is trying to send to the server, it is not sending the id (because one was probably never created). Here is the json that Ember is sending to my server

  gecko: { type: "alloc", date: "2015-05-30T13:28:27.539Z"}

My assumption was/is that I am supposed to save the id that Ember creates on my server (which would of course allow it to retrieve the record by id provide my server implements that).

Question: why is there no id being created?

this is the code

    App = Ember.Application.create();

    App.Router.map(function() {
      this.route("gecko", { path: "/" });

    });
    App.ApplicationAdapter = DS.RESTAdapter.extend({
    //haven't actually any created any code for this part yet
    });
    App.ApplicationStore = DS.Store.extend({
      adapter: App.ApplicationAdapter.create() 
    });
    App.Gecko = DS.Model.extend({
      type: DS.attr('string'),
      date: DS.attr('date')
    })
    App.GeckoRoute = Ember.Route.extend({

      model: function() {
       //currently does nothing. originally I tried to do `return this.store.find('gecko') but since there are no records yet on the backend, it's returning null which leads to an error which Array cannot map over
      },
    });
App.GeckoController = Ember.Controller.extend({

  actions: {
       createGeckoButtonClicked: function(){

            var gecko = this.store.createRecord('gecko', {
                date: new Date(),
                type: "gecko",                                     
            });
            console.log(gecko.id, "gecko.id before save"); //null
            gecko.save();
            console.log(gecko.id, "gecko.id"); //null
       }


  }

Note- not sure if it's relevant, I feel like I'm in a chicken/egg situation with the route where I can't return any entries because I haven't created any yet. so therefore, I'm trying to setup the ember app to be able to POST an entry to the server, then I will implement the route to retrieve it using return this.store.find('gecko'). As mentioned already if I do return this.store.find('gecko') with no entries in the db, it throws an error because array.prototype.map called on null or undefined




Aucun commentaire:

Enregistrer un commentaire