When it comes to persisting a model immediately, especially when one of its attributes is bound to a template input, the debate arises - should this functionality belong to the model or the controller?
In an attempt to address this dilemma, I devised a solution involving an observer:
# Models
App.Foo = DS.Model.extend
bars: DS.hasMany('bars')
App.Bar = DS.Model.extend
quantity: DS.attr('number')
# Template
{{#each bar in bars}}
{{input value=bar.quantity}}
{{/each}}
# Controller
persistQuantity: ( ->
@get('bars').forEach (bar) -> bar.save() if bar.get('isDirty')
).observes('[email protected]')
However, this implementation seems to trigger multiple save requests (in my case, 3) for the same model.
Another approach I experimented with was placing the observer directly on the model, but it resulted in an infinite loop:
# App.Bar Model
persistQuantity: ( ->
@save()
).observes('quantity')
Efforts to resolve this issue by utilizing Ember.run.once
were not successful, revealing gaps in my understanding of Ember's run loop mechanisms.