3 ms·
First of all, I'm really enjoying my time with Backbone! Unrelatedly, but in case anyone's listening, there's something I keep wanting in Backbone, but perhaps
by ender7 14y ago
First of all, I'm really enjoying my time with Backbone!
Unrelatedly, but in case anyone's listening, there's something I keep wanting in Backbone, but perhaps my architecture is wrong:
// inside my view
this.model.on('change', this._onChange, this);
// later
this.model.set({ ... }, null, this);
This example uses a hypothetical version of 'set', which won't trigger 'change' events for any listeners whose context is the third argument. So, my view won't receive a 'change' event after it changes the model.
Currently, when my view wants to change something, it modifies the model and then waits to receive the 'change' event before updating what the view actually looks like. However, this feels very indirect and doesn't always work out too well (sometimes I have to resort to setting a 'ignoreNextChangeEvent' variable).
- hoprocker 14y agoBackbone's philosophy is to keep things very loose, and let you decide how to couple Model<->View relationships; i.e., just setting a view's model doesn't explicitly update the view with new model data. I've had what sounds like a similar situation: I want to display on the view that the model is being updated. In this case, I set a flag on the current view when starting an action, then I watch out for 'sync' events on the model; when the 'sync' message is raised, I perform an action based on the set flags.
- deleted 14y ago[deleted]
- ender7 14y ago[Addendum] A common use case where this crops up is with animations. If the user directly interacts with my view and causes the model to change, I may want the view to play an animation or otherwise respond in a context-sensitive manner. However, I usually don't want to do this if some other view has changed the model (because it wouldn't make sense or because it's off-screen).