KnockoutJS数据绑定设置器 [英] KnockoutJS data-bind setter

查看:67
本文介绍了KnockoutJS数据绑定设置器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML数据绑定设置器有问题.我希望将其设置为model(exerciseCategories)时间间隔值. 如果我绑定到模型的区间,则它是正确的值,但不可观察. 如果我将其绑定到$ parent.intervals,则它是viewModel的默认值(1),但可以观察到. 我都想要:).我究竟做错了什么? 确实可以,但是显示[object Object]:

Having a problem with HTML data-bind setter. I want it to set to model(exerciseCategories) intervals value. If I bind to intervals from model it is the right value but is not observable. If I bind it to $parent.intervals it is default value (1) from viewModel but it is observable. I want both :). What am I doing wrong? Something like this does work but displays [object Object]:

<td data-bind='with: exercise'>
   <input data-bind='value: $parent.intervals(intervals)' />
</td>

What I've got is - HTML    
            ...
            <td>
                <select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: exerciseType'></select>
            </td>
            <td data-bind="with: exerciseType">
                <select data-bind='options: exercises, optionsText: "title", optionsCaption: "Izberite...", value: $parent.exercise'></select>
            </td>
            <td data-bind='with: exercise'>
                    <input data-bind='value: $parent.intervals' />
            </td>
            ...
 JavaScript
    var exerciseCategories = [
    {
        exercises: [{
            title: 'Aerobic exercise #1',
            intervals: 2
        }],
        category: 'Aerobics'
    }];

        var Exercise = function () {
                var self = this;

                self.exerciseType = ko.observable();
                self.exercise = ko.observable();
                self.intervals = ko.observable(1);
            };

推荐答案

在执行$ parent.intervals(intervals)时,您正在调用interval可观察函数,并将interval作为参数传递,显然,您将收到ko.observable对象作为结果.

Doing $parent.intervals(intervals) you are invoking the intervals observable function passing intervals as a parameter, and obviously you'll receive the ko.observable object as a result.

我摘录了你的摘录.看看这个 http://jsfiddle.net/MhHc4/

I got your excerpt working. Take a look at this http://jsfiddle.net/MhHc4/

HTML

Categories:
<select data-bind='options: exerciseCategories , optionsText: "category", optionsCaption: "Izberite...", value: selectedCategory'></select>
<p>selectedCategory() debug: <pre data-bind="text: selectedCategory() ? ko.toJSON(selectedCategory().exercises, null, 2) : ''"></pre>
</p>Exercises:
<select data-bind='options: selectedCategory() ? selectedCategory().exercises : [], optionsText: "title", value: selectedExercise'></select>
<p>selectedExercise() debug: <pre data-bind="text: selectedExercise() ? ko.toJSON(selectedExercise(), null, 2) : 'x'"></pre>
</p>
<input type="text" data-bind="attr : { value : selectedExercise() ? selectedExercise().intervals : 0 }"/>

JavaScript

Javascript

var exerciseCategories = [{
    exercises: [{
        title: 'Aerobic exercise #1',
        intervals: 2
    }],
    category: 'Aerobics'
}];

var ExerciseViewModel = function () {
    var self = this;

    self.exerciseCategories = ko.observable(exerciseCategories);
    self.selectedCategory = ko.observable();
    self.selectedExercise = ko.observable();
    self.intervals = ko.observable(1);
};
ko.applyBindings(new ExerciseViewModel());

HTH

这篇关于KnockoutJS数据绑定设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆