简体   繁体   中英

ember-data: How to POST properly

I've set up an ember.js client with ember-data as persistance handler. Fetching all records from the DB works fine with this code:

router.js

App.ProjectsRoute = Ember.Route.extend({
    model: function() {
        return this.store.find('project');
    }
});

But adding a new record doesn't work correctly with this code. It's only added to the model and not persisted.

index.html (example)

{{input type="text" id="newTitle" value=Title}}
<button class="small" {{action 'createProject'}}><i class="icon-plus-sign"></i></button>

Controller.js

App.ProjectsController = Ember.ArrayController.extend({
    actions: {
        createProject: function () {
            var project = this.store.createRecord('project', {
                project_number: this.get('newProject_number'),
                title: this.get('newTitle'),
                client: this.get('newClient'),
                comment: this.get('newComment'),
                xmlfile: this.get('newXmlfile')
            });
            this.set('newProject_number', '');
            this.set('newTitle', '');
            this.set('newClient', '');
            this.set('newComment', '');
            project.save();
        }
    }
});

models.js

App.Project = DS.Model.extend({
    title: DS.attr('string'),
    client: DS.attr('string'),
    comment: DS.attr('string'),
    project_number: DS.attr('string'),  
});

app.js

window.App = Ember.Application.create();

App.store = DS.Store.extend({
    adapter: DS.RESTAdapter,
});

DS.RESTAdapter.reopen({
  namespace: 'api/index.php',
  headers: {
      "API_KEY": "secret key",
      "ANOTHER_HEADER": "Some header value"
    }
});

Framework versions

Ember : 1.2.0; 
Ember Data : 1.0.0-beta.5+canary.e120006;
Handlebars : 1.1.2; 
jQuery : 2.0.3; 

What have I missed to configure? There's no error in the console however. REST-Api works well with curl.

The problem is the structure of the JSON "POST"ed towards the API:

Sent:

{project: {client: "test",comment: "test",project_number: "test",title: "test"}}

Expected by API-Backend:

{client: "test",comment: "test",project_number: "test",title: "test"}

I wonder where I could have found the information how ember-data build it's requests. (For further development)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM