1

I am trying to build a simple Todo List using all the last routes and data stuff for ember. You can find my full repo here

I have my store set up like so:

EmberTodo.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({bulkCommit: false})
});

The line of code that is giving me trouble comes from here:

EmberTodo.CreateItemView = Ember.TextField.extend({
  insertNewline: function() {
    EmberTodo.Item.createRecord({description: this.get('value')});
    this.set("value", "");
  }
});

From what I understand, calling createRecord doesn't create the record, but instead I need to call commit() somewhere. However, I cannot figure out where. Anyone have any ideas?

earnold
  • 1,440
  • 1
  • 15
  • 26

1 Answers1

2

From what I understand, calling createRecord doesn't create the record, but instead I need to call commit() somewhere. However, I cannot figure out where. Anyone have any ideas?

Sure. To get this working with the smallest possible

EmberTodo.CreateItemView = Ember.TextField.extend({
  insertNewline: function() {
    item = EmberTodo.Item.createRecord({description: this.get('value')});
    item.get('transaction').commit();
    this.set("value", "");
  }
});

I've placed a simplified, working example using DS.FixtureAdapter here: http://jsbin.com/ugipap/1/edit

Done, right?

Kinda. Thing is, you really don't want to be doing this kinda thing from within a view. Consider refactoring to move this logic into the controller layer, or possibly the router depending on the situation.

Mike Grassotti
  • 19,040
  • 3
  • 59
  • 57
  • Thanks for the prompt answer! That totally fixes it! But out curiosity, what would be the best way to bubble this up into the controller? – earnold Jan 23 '13 at 22:23
  • @earnold you have a controller for this view (perhaps `EmberTodo.ItemController`) with a method `createItem`. Then, in your view, call `this.get('controller').createItem({description: this.get('value')})`. – bantic Apr 17 '13 at 15:47
  • Hi @Mike, i have a question that in official Ember TODO example, they use `model.save()` to commit the chagne, so what's the difference between `model.save()` and `model.get('transaction').commit()` or `controller.get('store').commit()`? – YuC Aug 30 '13 at 06:50
  • `controller.get('store').commit()` will commits the default transaction of the controller's store. model.get('transaction').commit() commit's the model's transaction. If you've only got one store and have not created a transaction these are the same thing. `model.save()` does not commit right away - instead it schedules the model to be saved at the next run loop. So if you call `model.save()` on several models they all get batched into the same commit. See my answer here: http://stackoverflow.com/questions/17309934/difference-between-model-save-versus-model-getstore-commit – Mike Grassotti Aug 30 '13 at 07:27