jeudi 21 mai 2015

store.updateRecord is not a function in ember.js

I am newbie to ember.js, I am using ember-1.9.1.js and ember-data for my project. For back-end configuration I have created a REST API with core php and the db is MySQL. Now I can create new records (posts) from client side using with DS.RESTAdapter's "createRecord" function. But I don't know how to update a Record (post) with DS.RESTAdapter's "updateRecord" function. When I try to call the "updateRecord" function from "App.PostController (doneEditing)" I got this error:

Uncaught TypeError: store.updateRecord is not a function --------- app.js

app.js code below


App = Ember.Application.create();

App.IndexRoute = Ember.Route.extend({
  redirect: function() {
   this.transitionTo('home'); 
  }
});


App.ApplicationAdapter = DS.RESTAdapter.extend({

  namespace: 'pran/webapp/rest_adapter/api',

    createRecord: function(store, type, snapshot) {
        var data = this.serialize(snapshot, { includeId: true });
        var url = "api/new_post";
        return new Ember.RSVP.Promise(function(resolve, reject) {
          jQuery.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            data: data
          }).then(function(data) {
            Ember.run(null, resolve, data);
          }, function(jqXHR) {
            jqXHR.then = null; // tame jQuery's ill mannered promises
            Ember.run(null, reject, jqXHR);
          });
         });
    },
    updateRecord: function(store, type, record) {
        var data = this.serialize(snapshot, { includeId: true });
        var id = snapshot.id;
        var url = "api/update";
        return new Ember.RSVP.Promise(function(resolve, reject) {
          jQuery.ajax({
            type: 'PUT',
            url: url,
            dataType: 'json',
            data: data
          }).then(function(data) {
            Ember.run(null, resolve, data);
          }, function(jqXHR) {
            jqXHR.then = null; // tame jQuery's ill mannered promises
            Ember.run(null, reject, jqXHR);
          });
        });
      }

});



App.Store = DS.Store.extend({
  revision: 12,
  adapter: 'App.ApplicationAdapter'
});



App.Post = DS.Model.extend({
  title: DS.attr('string'),
  author: DS.attr('string'),
  date: DS.attr('date'),
  excerpt: DS.attr('string'),
  body: DS.attr('string')
});



App.Router.map(function() {
    this.resource('home');
    this.resource('about');
    this.resource('posts', function(){
      this.resource('post', { path: ':post_id' });
    });
    this.resource('newstory' , {path : 'story/new'});
});


App.PostsRoute = Ember.Route.extend({
  model: function() {
    return this.store.filter('post', { id: true }, function(post) {
      return post.get('id');
      return this.store.find('post');
    });
  }
});


App.PostRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.find('post', params.post_id);
  }
});


App.NewstoryController = Ember.ObjectController.extend({        
 actions :{
    save : function(){
        var title = $('#title').val();
        var author = $('#author').val();
        var excerpt = $('#excerpt').val();
        var body = $('#body').val();
        var store = this.get('store');
        var new_post = store.createRecord('post',{
            title : title,
            author : author,
            date : new(Date),
            excerpt : excerpt,
            body : body
        });
        new_post.save();
        this.transitionToRoute('posts');
    }
 }
});



App.PostController = Ember.ObjectController.extend({
      isEditing: false,  
      actions: {      
        edit: function() {
          this.set('isEditing', true);
        },
        doneEditing: function() {
          this.set('isEditing', false);         
          var title = $('#title').val();
          var excerpt = $('#excerpt').val();
          var body = $('#body').val();                
          var store = this.get('store');                    
          var update_post = store.updateRecord('post',{
                title : title,
                excerpt : excerpt,
                body : body
            });
          update_post.save();
    }
 }
});

Somebody please suggest a way to fix the issue.




Aucun commentaire:

Enregistrer un commentaire