5

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')
Gordon Isnor
  • 2,065
  • 1
  • 19
  • 32

1 Answers1

7
 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();
 }
flylib
  • 1,128
  • 2
  • 10
  • 17
  • Wonderful! Huge help. How can I find more about this facility, "this.container.lookup" etc.? I'm having a hard time finding anything in the guides or the API docs. – hourback Mar 12 '15 at 21:24
  • 1
    it's a private api (only source code, no docs other then comments in code) but sometimes needed to reach at to do certain things in your app, you can look up anything in the app with it but you run the risk of that causing problems between new ember upgrades if it gets changed somehow as private apis can change as much as they feel fit so it isn't recommended to use a lot, only in certain situations from time to time like this, mostly i just use it to look up routes in controllers from time to time that i need to interact with – flylib Mar 13 '15 at 05:04