2013-03-17 162 views
0

我試圖用淘汰賽映射插件,但它不是將數據綁定到我的UI。以下是我的代碼:問題與淘汰賽映射插件

var model; 

$(document).ready(function() { 
$.ajax({ 
    type: "POST", 
    url: SERVER_PATH + '/jqueryservice/DataAccessService.asmx/GetData', 
    async: false, 
    data: "{ }", 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (result, status) { 
     model = ko.mapping.fromJS(result.d); 
    }, 
    error: GetDataError 

}); 

}); 

function GetDataError() { 
    alert("System is not responding at the moment. Please try again."); 
} 

任何人都可以請讓我知道我在做什麼錯在這裏?以下是這是一個從服務器返回的數據:

{"ID":903,"Name":"Camela Holding","Email":"[email protected]", 
    "Vehicles":[{"ID":1,"Name":"Van"}, 
       {"ID":2,"Name":"Car"}, 
       {"ID":4,"Name":"Cadillac"}, 
       {"ID":5,"Name":"Train"},       {"ID":3,"Name":"Jeep"}], 
    "OwnerVehicle":0,"WeeklyData":null,"Week1":null} 
+1

try model = ko.mapping.fromJS(result); – 2013-03-17 06:39:01

+2

你在哪裏調用'ko.applyBindings(model);'? – nemesv 2013-03-17 06:39:40

+0

你有什麼行爲?錯誤警報或不正確的輸出?你的結果沒有'd'。爲什麼KO模型綁定到'result.d'? – 2013-03-17 06:45:16

回答

0
<table width="100%" class="mGrid" border="1"> 
    <tr> 
    <th colspan="4"> 
    </th> 
    </tr> 
    <tr> 
    <td style="width:15%;">Customer ID:</td> 
    <td style="width:35%;" colspan="3"> 
     <input id="cusNo" maxlength="10" type="text" data-bind="value: ID" /> 
    </td> 
    </tr> 
    <tr> 
    <td style="width:15%;">Email:</td> 
    <td style="width:35%;"> 
     <input id="nic" maxlength="10" type="text" data-bind="value: Email" /> 
    </td> 
    <td style="width:15%;">Name:</td> 
    <td style="width:35%;"> <input id="name" maxlength="50" type="text" data-bind="value: Name" /> 
    </td> 
    </tr>  

    <tr> 
    <td style="width:15%;">Vehicles:</td> 
    <td style="width:35%;"> 
     <select id="status" data-bind="options:Vehicles, value:OwnerVehicle, optionsText: 'Name', optionsValue: 'ID'"> 
     </select> 
    </td> 
    <td style="width:15%;">Owner Vehicle:</td> 
    <td style="width:35%;"> <input id="Text1" maxlength="50" type="text" data-bind="value: OwnerVehicle" /> 
    </td> 
    </tr> 
</table> 

這是我的小提琴,它顯示了精確的輸出我在運行時獲得。

http://jsfiddle.net/N2G7w/

欣賞一些幫助。

0

我能夠進行此修復工作中有類似的情況,但我不知道確切的原因,以及它是如何工作的。

我試圖結合可觀察到的與這是一個陣列,利用敲除映射插件AJAX響應。

這是我在做什麼起初:

self.DataRows = ko.observableArray(); 
    $.ajax({ 
       url: '/Home/GetStudentList1', 
       cache: false, 
       type: 'post', 
       data: self.GridParams, 
       success: function (data) {       
        self.DataRows(ko.mapping.fromJS(data)); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        console.log("error :" + XMLHttpRequest.responseText); 
        alert('There was an error in performing this operation.'); 
       } 
      }); 

該數據可觀察得到映射,但是在UI當時並沒有更新我想這

$.ajax({ 
       url: '/Home/GetStudentList1', 
       cache: false, 
       type: 'post', 
       data: self.GridParams, 
       success: function (data) { 
        var observableData = ko.mapping.fromJS(data); 
        self.DataRows(observableData()); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        console.log("error :" + XMLHttpRequest.responseText); 
        alert('There was an error in performing this operation.'); 
       } 
      }); 

,唯一不同的是我在將它與我的可觀察值進行映射之前,將它放入一個變量中
同樣你的情況,我想你應該嘗試這樣的:

var self = this; //save reference to this as you will it in ajax success 
self.model = ko.observable(); //create your observable 
$.ajax({ 
      type: "POST", 
      url: SERVER_PATH + '/jqueryservice/DataAccessService.asmx/GetData', 
      async: false, 
      data: "{ }", 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
     success: function (result, status) { 
       var data = ko.mapping.fromJS(result.d);// don't have any idea what is making knockout happy doing it this way? 
       self.model(data()); 
    }, 
     error: GetDataError 

    }); 

在這兩種情況下,當我在做的console.log(數據行())可觀察的結構是一樣的。

0

那麼這裏是回答我們的問題 -

試試這個

var model = ko.observable(); 
$(document).ready(function() { 
$.ajax({ 
     type: "POST", 
     url: SERVER_PATH + '/jqueryservice/DataAccessService.asmx/GetData', 
     async: false, 
     data: "{ }", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
success: function (result, status) { 
    model(ko.mapping.fromJS(result.d)()); //mapped using braces() 
}, 
error: GetDataError 

    }); 

}); 

注意這裏唯一的區別是,我做你的模型和觀察到的,而不是 模型= ko.mapping.fromJS( result.d)); 我用 模型(ko.mapping.fromJS(result.d)());

而不是我的JSON對象映射到數組現在我映射我observable.As你必須知道,所有觀察到的變功能和使用這些括號訪問他們的價值觀「()」之類的功能。

只因爲這個原因,當我把它變成一個獨立的變量,然後使用大括號訪問它,它在工作。