Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping the route's model

I have an app with a cart displayed on top of the page, in the application template. Then, in the application route I define the model, along the lines of:

App.ApplicationRoute = Em.Route.extend
  model: (-> 
    @store.find('cart', @session.get('cart_id')
  ).observes('session.cart_id')

When a user logs in, I want to replace that cart with the one loaded from the server. What is the best way to approach this?

I got to the point, where the cart is loaded to the Ember Data store, the observer block gets executed, but the template is not updated with the new model. Or, maybe, I should use something entirely different for this?

like image 241
Greg Funtusov Avatar asked Mar 25 '26 13:03

Greg Funtusov


1 Answers

the model hook is called by the router while hooking up models for the current route. If you'd like to change the model on a controller, just grab the controller and set the model property to the new model.

Assuming the session exists in the application route

App.ApplicationRoute = Em.Route.extend
  updateModel: (-> 
    @store.find('cart', @session.get('cart_id')).then (record) => 
      @controller.set('model', record)
  ).observes('session.cart_id')

(I mixed and matched coffeescript, I only know it from stackoverflow questions, so sorry if it's wrong)

Example: http://emberjs.jsbin.com/wedup/1/edit

like image 53
Kingpin2k Avatar answered Mar 27 '26 08:03

Kingpin2k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!