knockout.js
  • webforms
  • 2012-11-05 176 views 0 likes 
    0

    我在這裏抨擊我的頭靠在牆上,試圖弄清楚爲什麼我無法使它工作。我已經嘗試了以下很多例子,並將我所嘗試的所有內容都剝離到了我的下面。使用ASP.NET Webforms KnockoutJS

    所以,我的aspx頁面上,我有:

    <input type="radio" data-bind="value: individual" />Individual

    這裏是我的javascript:

    var serviceBase = 'http://localhost:49906/PopulationSelection.aspx/'; 
    var getSvcUrl = function (method) { return serviceBase + method; }; 
    
    var ajaxGetJson = function (method, jsonIn, callback) { 
        $.ajax({ 
         url: getSvcUrl(method), 
         type: "GET", 
         data: ko.toJSON(jsonIn), 
         dataType: "json", 
         contentType: "application/json", 
         success: function (json) { 
          callback(json.d); 
         }, 
         error: function (xhr, ajaxOptions, thrownError) { 
          alert(xhr + ' ' + thrownError); 
         } 
        }); 
    } 
    
    var batchesDataService = { 
        getSavedBatches: function (callback) { 
         ajaxGetJson("GetBatches", null, callback); 
        } 
    }; 
    
    var Batch = function (p) { 
        this.individual = ko.observable(p.Individual); 
        this.household = ko.observable(p.Household); 
        this.countOnly = ko.observable(p.CountOnly); 
        this.femalePrimary = ko.observable(p.FemalePrimary); 
        this.eventManagement = ko.observable(p.EventManagement); 
        this.eventManagementText = ko.observable(p.EventManagementText); 
        this.randomSampling = ko.observable(p.RandomSampling); 
        this.randomSamplingText = ko.observable(p.RandomSamplingText); 
        this.stateHasChanged = ko.observable(false); 
    }; 
    
    var loadBatchesCallback = function (data) { 
        var parsed = JSON.parse(data); 
        myViewModel.Batch = new Batch(parsed); 
        //also tried: 
        //myViewModel.Batch(new Batch(parsed)); 
    }; 
    
    var myViewModel; 
    var viewModel = function() { 
        this.Batch = ko.observable(); 
        this.getBatchInfo = function() { 
         batchesDataService.getSavedBatches(loadBatchesCallback); 
        }; 
    }; 
    
    $(document).ready(function() { 
        myViewModel = new viewModel(); 
        myViewModel.getBatchInfo(); 
        ko.applyBindings(myViewModel.Batch); 
    }); 
    

    我有從我的webmethod獲取數據回沒問題(使用會話訪問),當我提醒批處理成員時,我會看到正確的信息。

    我的問題來自實際ko.applyBindings()。無論我嘗試了什麼,我在控制檯中出現以下錯誤:

    Uncaught Error: Unable to parse bindings. 
    Message: ReferenceError: individual is not defined; 
    Bindings value: value: individual 
    

    任何幫助表示讚賞!

    回答

    2

    異步性。

    在調用ajax回調之前調用ko.applyBindings,所以它沒有值。

    您可能想要在回調中的某處調用applyBindings。

    +0

    這樣做。我改變了loadBatchesCallback,如下所示: 'var loadBatchesCallback = function(data){ var parsed = JSON.parse(data); myViewModel.Batch = new Batch(parsed); ko.applyBindings(myViewModel.Batch); };' – JCompo

    +0

    我忘了說謝謝! – JCompo

    0

    首先,我想你想

    ko.applyBindings(myViewModel.Batch); 
    

    您使用批處理的不一致,以及。你在一個地方將它定義爲一個可觀察對象,但不會更新它。我建議遵循以大寫開頭的類定義和小寫的實例/屬性變量的約定。這可能有助於澄清你在哪裏使用什麼類型的東西。

    +0

    hrmm ...我不知道它是如何變回viewModel.Batch。我將其更改爲myViewModel.Batch再次具有相同的結果。對於混音我很抱歉。 – JCompo

    相關問題