2013-04-06 47 views
0

我正在對服務器的呼叫通過Ajax請求得到一些數據promisses訪問結果電話和我使用的承諾。雖然我的模板在數據返回後自動更新,但我不太習慣訪問控制器中的數據,因爲我不知道它到底在哪裏。燼阿賈克斯在模板和控制器

要解決我可以在控制器的額外Ajax調用來獲得相同的數據,但那種感覺難看。是否有更好的方法來知道何時訪問控制器中的數據?作爲解決方法,我嘗試調用需要didInsertElement上的數據的函數,但這並沒有解決問題。

App.ActiveDataSet = Ember.Object.extend({ 
    progress: 0 
}); 

App.ActiveDataSet.reopenClass({ 
    findAll: function(project_id) { 
    var result = []; 
    $.ajax({ 
     url: '/active_data_sets.json', 
     type: 'GET', 
     data: {'project_id': project_id} 
    }).then(function(response) { 
     response.active_data_sets.forEach(function(newset) { 
      result.addObject(App.ActiveDataSet.create(newset)); 
     }); 
    }); 

    return result; 
    } 
}); 

App.MapviewShowRoute = Ember.Route.extend({ 
    setupController: function(controller, model) { 
    this.controllerFor('activedatasetIndex').set('content', App.ActiveDataSet.findAll(model.id)); 
    } 
}); 

App.MapviewShowController = Ember.ObjectController.extend({ 
    needs: ['activedatasetIndex'], 

    content: null, 
    dataSets: [], 

    createDataSets: function() { 

    // create the datasets 
    for (var counter = 0; counter < this.get('controllers.activedatasetIndex.content').length; counter++) { 
     alert(dataSets[counter].ds.name); 
    } 
    } 

}); 

App.MapviewShowView = Ember.View.extend({ 
    map: null, 

    didInsertElement: function() { 
    var map = null; 

    var myOptions = { 
     zoom: 8, 
     center: new google.maps.LatLng(52.368892, 4.875183), 
     mapTypeControl: true, 
     mapTypeControlOptions: { 
     style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
     }, 
     navigationControl: true, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    map = new google.maps.Map(this.$('#map_canvas').get(0), myOptions); 


    this.set('map', map); //save for future updations 

    var h = $(window).height(), 
     offsetTop = 60; // Calculate the top offset 

    $('#map_canvas').css('height', (h - offsetTop)); 

    // get the datasets 
    this.controller.createDataSets(); 
    } 
}); 

回答

0

您是否嘗試過持續的承諾鏈:

App.ActiveDataSet.reopenClass({ 
    findAll: function(project_id) { 
    return $.ajax({ 
     url: '/active_data_sets.json', 
     type: 'GET', 
     data: {'project_id': project_id} 
    }).then(function(response) { 
     var result = []; 
     response.active_data_sets.forEach(function(newset) { 
      result.addObject(App.ActiveDataSet.create(newset)); 
     }); 
     return result; 
    }); 
    } 
}); 

App.MapviewShowRoute = Ember.Route.extend({ 
    setupController: function(controller, model) { 
    App.ActiveDataSet.findAll(model.id).then(function(content) { 
     this.controllerFor('activedatasetIndex').set('content', content); 
    }); 
    } 
});