0

In the process of writing my first Ember-app, I have not been able to figure out how to POST a new resource to my API using the REST Adapter.

The app successfully retrieves a list of resources and displays it, so it seems my Model is defined correctly and the communication between the Adapter and the API works. When I click on the CreateBugView-button, the new instance shows up in the list, so this part works as well. However, I can see in the inspector that no POST-request is made to the API and when I refresh the page, the instance is, predictably, nowhere to be seen.

I am using the 1.0.0-rc.1 version of Ember and a version of Ember Data that I cloned and built today.

Below is my code, I'd be grateful to anyone who could help me figure out what's wrong.

App = Em.Application.create()

# Templates

require 'templates/application'
require 'templates/index'

# Models

App.Bug = DS.Model.extend
  name: DS.attr 'string'

# Views

App.CreateBugView = Em.View.extend
  tagName: 'button'

  click: (evt) ->
    bug = App.Bug.createRecord
      name: 'Sean'

# Routes

App.IndexRoute = Em.Route.extend
  model: -> App.Bug.find()
  setupController: (controller, bugs) ->
    controller.set 'bugs', bugs
    controller.set 'App', App

# Store

App.Store = DS.Store.extend
  revision: 12

# Router

App.Router.map ->
  @route 'index', path: '/'

App.initialize()

Index-template

<ul>
  {{#each bugs}}
    <li>{{name}}</li>
  {{/each}}
</ul>

{{#view App.CreateBugView}}
  Create new bug!
{{/view}}
j.koch
  • 8
  • 3
  • 8

1 Answers1

1

I believe that the model only gets persisted once commit() is called on the store.

Obviously, I can't fully test without building the REST api, but using the fixture adapter, I can call a save() method on my controller, which pushes the @store.commit(). For a rest adapter, this should produce the POST event.

Please have a look at this jsFiddle: http://jsfiddle.net/nrionfx/uqRG5/5/

  click: (evt) ->
    bug = App.Bug.createRecord
      name: 'Sean'
    @.get('controller').save()

and

App.IndexController = Em.ArrayController.extend
    save: ->
        console?.log("Commiting changes to store")
        @store.commit()
nrion
  • 248
  • 1
  • 7
  • Thanks for tipping me off to need to commit, I found this answer which pretty much matches my question exactly: http://stackoverflow.com/a/14490259/677985 Your way seems cleaner from an MVC-separation point of view though, I'll definitely look into the ArrayController as well. – j.koch Mar 12 '13 at 20:30