dimanche 29 janvier 2017

how to update model from the Route function

I am trying to write small search application but am struck at updating the model based on some action.


import Ember from 'ember';

model/index.js: import DS from 'ember-data';

 export default  DS.Model.extend({
 searchText: DS.attr('string'),
 title: DS.attr('string'),
 description: DS.attr('string'),
 url: DS.attr('string'),
 source: DS.attr('string')
});

route/index.js: export default Ember.Route.extend({

  actions:{

    submitsearch: function(inputsearchtext) {
    var data = {
      "searchText": inputsearchtext
    }

     var models = this.model.values;
     alert(models);
    Ember.$.ajax('http://c6403:2222/server/getkb', {
      type: 'POST',
      dataType: 'json',
      contentType: "application/json",
      data: JSON.stringify(data),
      async: false,
      context: this,
  }).then(function(response) {
      var type = response.type;
      var payload = response.payload;
      alert(payload);
      this.store.pushPayload(type, payload);
   });

   }

 }
});


Basically I wanted to update my model based on the searchtext user has provided. As part of this I am making "submitsearch" call and from there trying to make a AJAX/json call - once I get the json response I will have to update model so that my view will be updated/refreshed.

here is my questions:

  1. json response "payload" is undefined though server returns proper response - I could validate the response in browser.

  2. after getting the payload what is the correct way to update the model so that component should be refreshed to show the results.




Aucun commentaire:

Enregistrer un commentaire