Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

javascript - Unable to parse bindings js error using ko.mapping.fromJSON with parse exist view model

I want to save view model in the hidden field in JSON format. All work fine.

But when I try to get it - I get error:

Uncaught Error: Unable to parse bindings. Message: ReferenceError: selectAll is not defined; Bindings value: checked: AllCheck, click: selectAll

JsFiddler

viewModel

function AppViewModel() {

//Week
this.AllCheck = ko.observable(false);
this.DaysOfWeekResult = ko.observableArray();

this.selectAll = function () {
    if (this.AllCheck()) {
        viewModel.DaysOfWeekResult.removeAll();

        viewModel.DaysOfWeekResult.push("Mo");
        viewModel.DaysOfWeekResult.push("Tu");
        viewModel.DaysOfWeekResult.push("We");
        viewModel.DaysOfWeekResult.push("Th");
        viewModel.DaysOfWeekResult.push("Fr");
        viewModel.DaysOfWeekResult.push("Sa");
        viewModel.DaysOfWeekResult.push("Su");
    }

    return true;
};

    this.dayClicked = function () {
    checkDays();
    return true;
};        

}

Init code

var viewModel;    
$().ready(function (){
        viewModel = new AppViewModel();
var viewModelDeserialized = ko.mapping.fromJSON(serializedJsonString, viewModel);

        ko.applyBindings(viewModel);
}); 


function checkDays() {
    viewModel.AllCheck(viewModel.DaysOfWeekResult().length == 7);   
}  

Serialized model

var serializedJsonString = '"AllCheck":false,"DaysOfWeekResult":[]}';

MarkUp

<div class="check-block">
                <input name="AllWeek" id="AllWeek" type="checkbox" value="AllWeek" data-bind="checked: AllCheck, click: selectAll" />
                <label for="AllWeek">All Week</label>
            </div>
            <div class="holder">
                <span>
                    <input name="Monday" id="Monday" type="checkbox" value="Mo" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Monday">Mo</label>
                </span><span>
                    <input name="Tuesday" id="Tuesday" type="checkbox" value="Tu" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Tuesday">Tu</label>
                </span><span>
                    <input name="Wednesday" id="Wednesday" type="checkbox" value="We" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Wednesday">We</label>
                </span>               
                <span>
                    <input name="Thursday" id="Thursday" type="checkbox" value="Th" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Thursday">Th</label>
                </span><span>
                    <input name="Friday" id="Friday" type="checkbox" value="Fr" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Friday">Fr</label>
                </span><span>
                    <input name="Saturday" id="Saturday" type="checkbox" value="Sa" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Saturday">Sa</label>
                </span><span>
                    <input name="Sunday" id="Sunday" type="checkbox" value="Su" data-day="true" data-bind="checked: DaysOfWeekResult, click: dayClicked" />
                    <label for="Sunday">Su</label>
                </span>
            </div>

?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are calling the ko.mapping.fromJSON with wrong arguments.

The correct usage in your case is the following:

var viewModelDeserialized = 
    ko.mapping.fromJSON(serializedJsonString, {} /* empty options */, viewModel);

Demo fiddle. (without the binding error)

The usage of the ko.mapping.fromJSON method is a little bit tricky:

  • you can call it with one argument: providing just the data e.g var viewModel = ko.mapping.fromJSON(data) in this case it will return the created viewModel

  • you can call with two arguments:

    • if the second argument is a ko mapping created viewModel then it is treated as the mapping target ko.mapping.fromJSON(data, koMappingCreatedViewModel)
    • otherwise the second argument is treated as the mapping options (this happens in your case) var viewModel = ko.mapping.fromJSON(data, options)
  • you can call it with three arguments explicitly specifing the data, mapping and target: ko.mapping.fromJSON(data, options, target)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.9k users

...