2012-08-07 85 views
2

我對Backbone非常陌生,我一直在嘗試使用ASMX web服務創建自動完成功能。我似乎遇到的問題是,雖然我的web服務在JSON中返回(經過與它的痛苦戰鬥),但它將響應包裝在'd'(數據集)中。我如何才能理解這一點並獲取正確的數據?使用backbone.js從'ASMX'獲取數據'd'

這裏是我的代碼: -

var Airline = Backbone.Model.extend({ 
       initialize: function() {}, 
       defaults: { 
        name: 'Airline Name', 
        rating: 50, 
        icon: '/blank.png' 
       } 
      }); 

      var AirlineCollection = Backbone.Collection.extend({ 
       model: Airline, 
       contentType: "application/json", 
       url: '/ControlTower/public/runway.asmx/all-airlines', 
       parse: function (response) { 
        return response; 
       } 
      }); 

      var SelectionView = Backbone.View.extend({ 
       el : $('#airline'), 
       render: function() { 
       $(this.el).html("You Selected : " + this.model.get('AirlineName')); 
       return this; 
       }, 
      }); 

var airlines = new AirlineCollection(); 
      airlines.fetch({async: false, contentType: "application/json" }); 
      var airlineNames = airlines.pluck("AirlineName"); 

$("#airline").autocomplete({ 
       source : airlineNames, 
       minLength : 1, 
       select: function(event, ui){ 
       var selectedModel = airlines.where({name: ui.item.value})[0]; 
       var view = new SelectionView({model: selectedModel}); 
       view.render(); 
       } 
      }); 

有人能看到我在做什麼錯?我一直坐在這裏太久了!

幫助表示讚賞;)

+0

閱讀本 - HTTP:/ /encosia.com/a-breaking-change-between-versio ns-of-aspnet-ajax/ – 2012-08-07 11:34:42

回答

4

怎麼樣在你的AirlineCollection

parse: function (response) { 
    return response.d; 
} 
+0

Doh!只是票:)非常感謝。 – dooburt 2012-08-07 20:46:46

0

這對我的作品

AirlineCollection

parse: function (response) { 
    var data = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d; 
    return data;   
} 
相關問題