Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember update parent route's model after save record

I'm working on an Ember application with the intent of learning. I've setted on a UI where a parent route and template when first opened features a table of, for example, today’s items in the left hand column, and an area to the right that varies: details about an item, form for new/edit item, search, etc.

The trouble I have run into is that when I save a new item, the left hand table from the parent route is not updated with the new item. Having trouble finding a way of getting that route to refresh. The closet I came was by using pushObject on the model.

things template:

{{partial "things/table"}}
{{outlet}}

router.coffee

@resource "items",  ->
      @route "item", {path: "/:item_id"}
      @route "new", {path: "/new"}

items route:

ItemsRoute = Ember.Route.extend(
    model: -> @store.find 'item'
)

items new route:

ItemsNewRoute = Ember.Route.extend

    renderTemplate: -> 
        this.render('items/form')

    model: ->
        @store.createRecord('item')

    setupController: (controller, model)->
        controller.set('model', model)

items new controller:

ItemsNewController = Ember.ObjectController.extend(
    needs: 'items'

    actions:
        submit: -> @model.save().then(console.log('saved'(, console.log('failed'))              
        cancel: -> @transitionTo('items')

    transitionAfterSave: (->
        if @get('content.id')       
            @transitionToRoute('items.item', @get('content')) 
    ).observes('content.id')
like image 628
Gordon Isnor Avatar asked Jan 15 '15 20:01

Gordon Isnor


1 Answers

 submit: -> @model.save().then(this.didSave.bind(this), console.log('failed'));

 didSave: function() {
     console.log('saved');
     var route = this.container.lookup("route:items.index"); // use the name of route you want to refresh
     route.refresh();
 }
like image 144
flylib Avatar answered Nov 04 '22 19:11

flylib