mercredi 9 mars 2016

triggering a function in component in Ember

When i'm transiting to my new route, i set reportName property and i want based on observing reportname change postobject and trigger sending ajax request. on the response of ajax i get data which i want to pass to my graph component as a property and this updated property with data value cause a function in my component to be triggered. But im not able to do so. Can you please let me know what i am doing wrong. I am new with ember. Pod Controller:

export default Ember.Controller.extend(ApplicationRouteMixin, {
    sessionService: Ember.inject.service('session'),

    nameOfReport: null,  

    postObject: function(){
        return {
        Name: this.get('nameOfReport'),
        Take: 30,
        Skip: 0,
    };
    }.property('nameOfReport'),


    ajaxResponse: function(){

       var restApiHost = serverPath + '/report’;
       var postobj=  this.get('postObject');
       var token =this.get('sessionService.session.authenticated.access_token');

        Ember.$.ajax({
        url: restApiHost,
        type: 'POST',
        data: JSON.stringify(postobj),
        contentType: 'application/json; charset=utf-8',
        dataType: "json",
        beforeSend: function (xhr) {
          var bearer = 'Bearer ' + token ;
            xhr.setRequestHeader('Authorization', bearer);
            xhr.setRequestHeader('Content-type', 'application/json');
        },
        success: function (data) {
        var graphobj= {
        data:  data.data,          
      graphModel: GraphModel.create({
            graphHeight: "320",
            graphMargin: [40, 80, 40, 60],
            xDomain: [1, 10],
            yDomain: [1, 100]
…
        })};

       Ember.set(this,'graphobject', graphobj);    
        },

        fail: function () {
            alert('Could not contact server');
        },

    });
    }.property('postObject'),       


    graphobject: function(){
this.get('ajaxResponse');
    }.property('ajaxResponse'),

});

Pod Template:

<div>
               {{line-graph model= graphobject }} 
</div>

Component:

drawGraph: function() {
        // define dimensions of graph   
        var graphdata = this.get('model.data');
        var graphmodel = this.get('model.graphModel');
...
}.property('graphobject'),




Aucun commentaire:

Enregistrer un commentaire