ko.subscribe 子模型属性

发布于 2025-01-03 10:06:25 字数 1859 浏览 1 评论 0原文

寻找如何在 knockoutjs 中设置子模型的好例子。这包括绑定到子事件,例如我还无法开始工作的属性更新。

另外,在这种情况下最好绑定到单个子项而不是数组,但我不知道如何在没有 foreach 模板的情况下在 html 中设置它。

http://jsfiddle.net/mathewvance/mfYNq/

谢谢。

<div class="editor-row">
    <label>Price</label>
    <input name="Price" data-bind="value: price"/>
</div>

 <div class="editor-row">
    <label>Child</label>
    <div data-bind="foreach: childObjects"> 
        <div><input type="checkbox" data-bind="checked: yearRound" /> Year Round</div>
        <div><input type="checkbox" data-bind="checked: fromNow" /> From Now</div>
        <div>
            <input data-bind="value: startDate" class="date-picker"/> to 
            <input data-bind="value: endDate" class="date-picker"/>
        </div>
    </div>
</div>
var ChildModel= function (yearRound, fromNow, startDate, endDate) {
    var self = this;

    this.yearRound = ko.observable(yearRound);
    this.fromNow = ko.observable(fromNow);
    this.startDate = ko.observable(startDate);
    this.endDate = ko.observable(endDate);

    this.yearRound.subscribe = function (val) {
        alert('message from child model property subscribe\n\nwhy does this only happen once?');

        //if(val){
        //    self.startDate('undefined');
        //    self.endDate('undefined');
        //}
    };
}

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

    this.price = ko.observable(1.99);
    this.childObjects = ko.observableArray([ new ChildModel(true, false) ]);
};

var viewModel = new ParentModel ();
ko.applyBindings(viewModel);

Looking for a good example of how to set up child models in knockoutjs. This includes binding to child events such as property updates which I haven't been able to get working yet.

Also, it would be better to bind to a single child in this case instead of an array but I don't know how to set it up in the html without the foreach template.

http://jsfiddle.net/mathewvance/mfYNq/

Thanks.

<div class="editor-row">
    <label>Price</label>
    <input name="Price" data-bind="value: price"/>
</div>

 <div class="editor-row">
    <label>Child</label>
    <div data-bind="foreach: childObjects"> 
        <div><input type="checkbox" data-bind="checked: yearRound" /> Year Round</div>
        <div><input type="checkbox" data-bind="checked: fromNow" /> From Now</div>
        <div>
            <input data-bind="value: startDate" class="date-picker"/> to 
            <input data-bind="value: endDate" class="date-picker"/>
        </div>
    </div>
</div>
var ChildModel= function (yearRound, fromNow, startDate, endDate) {
    var self = this;

    this.yearRound = ko.observable(yearRound);
    this.fromNow = ko.observable(fromNow);
    this.startDate = ko.observable(startDate);
    this.endDate = ko.observable(endDate);

    this.yearRound.subscribe = function (val) {
        alert('message from child model property subscribe\n\nwhy does this only happen once?');

        //if(val){
        //    self.startDate('undefined');
        //    self.endDate('undefined');
        //}
    };
}

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

    this.price = ko.observable(1.99);
    this.childObjects = ko.observableArray([ new ChildModel(true, false) ]);
};

var viewModel = new ParentModel ();
ko.applyBindings(viewModel);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2025-01-10 10:06:25

尝试以下操作:

this.yearRound.subscribe(function (val) {
        alert('value change');
    });

如果您想让订阅者在加载页面时也被调用,请执行以下操作:

var ChildModel= function (yearRound, fromNow, startDate, endDate) {
    var self = this;

    this.yearRound = ko.observable();
    this.fromNow = ko.observable(fromNow);
    this.startDate = ko.observable(startDate);
    this.endDate = ko.observable(endDate);

    this.yearRound.subscribe(function (val) {
        alert('value change');
    });
    this.yearRound(yearRound);
}

http ://jsfiddle.net/azQxx/1/ - 这对我来说适用于 Chrome 16 和 Firefox 10

每次选中的按钮更改其值时都会触发回调。

在我看来,如果您可能有多个子模型与父模型关联,那么 observableArray 就很好。

Try it with the following:

this.yearRound.subscribe(function (val) {
        alert('value change');
    });

If you want to have the subscriber also being called while loading the page do something like this:

var ChildModel= function (yearRound, fromNow, startDate, endDate) {
    var self = this;

    this.yearRound = ko.observable();
    this.fromNow = ko.observable(fromNow);
    this.startDate = ko.observable(startDate);
    this.endDate = ko.observable(endDate);

    this.yearRound.subscribe(function (val) {
        alert('value change');
    });
    this.yearRound(yearRound);
}

http://jsfiddle.net/azQxx/1/ - this works for me with Chrome 16 and Firefox 10

Every time the checked button changes its value the callback fires.

The observableArray is fine in my opinion if you may have more than one child model associated to the parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文