最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Applying knockout extenders after mapping - Stack Overflow

programmeradmin5浏览0评论

I'm having a problem with adding a knockout extender to observables after they have been created. In the following example the extender is run on foo every time the value changes as expected but only once, when first called, for bar.

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

    self.foo = ko.observable(1).extend({ numeric: 1 });

    self.bar = ko.observable(1);

    self.bar.extend({ numeric: 1 });
};

Essentially I am mapping a large JSON object and would like to add extenders after the mapping has occurred to some of the properties. Is there a simple way to do this?

Below is a jsfiddle showing the problem:

/

I'm having a problem with adding a knockout extender to observables after they have been created. In the following example the extender is run on foo every time the value changes as expected but only once, when first called, for bar.

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

    self.foo = ko.observable(1).extend({ numeric: 1 });

    self.bar = ko.observable(1);

    self.bar.extend({ numeric: 1 });
};

Essentially I am mapping a large JSON object and would like to add extenders after the mapping has occurred to some of the properties. Is there a simple way to do this?

Below is a jsfiddle showing the problem:

http://jsfiddle/LgxTn/

Share Improve this question edited Jun 7, 2013 at 14:40 ASGM 11.4k1 gold badge37 silver badges54 bronze badges asked Jun 7, 2013 at 14:14 sWWsWW 5974 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

The problem you have is that the extender is creating a new observable (based on the observable it's extending). And that new observable is not being used in your second call to extend.

To fix this (without changing the code of the extender) you can assign the response from calling extend into the same property it's called.

In other words, add self.bar = in front of your last line in order to have this:

self.bar = self.bar.extend({ numeric: 1 });
发布评论

评论列表(0)

  1. 暂无评论