ko.computed函数不会更新为“destroy”

时间:2015-11-18 19:48:36

标签: javascript jquery knockout.js tags

我有一个文本和一个标签对象,通过textID属性松散耦合。

为了收集特定文本的标签,我有一个功能“self.getTags”进行匹配。标签在我的视图中与Ryan Niemeyers出色的ko.sortable库一起呈现。这很好用。

但是,当我在我的标签上按下removeTag按钮(“click:function(){$ root.removeTag($ data)};”)时,“self.getTags”功能的结果不会更新。任何提示高度赞赏

这是代码:

Text = function(data){
    var self = this;
    self.textID = data.textID;
    self.projectID = data.projectID;
};

Tag = function(data){
    var self = this;
    self.ID = data.ID;
    self.text = ko.observable(data.text);
    self.projectID = data.projectID;
    self.textID = data.textID;
    self._destroy = data._destroy || false;
};

viewModel = function(){

    //some code…

    self.selectedProject = ko.observable();
    self.Tags = ko.observableArray().syncWith("Tags", true);               
    self.projectTags = ko.computed(function () {
        var project = self.selectedProject();
        if (!project) {
            return [];
        }
        else {
            return ko.utils.arrayFilter(self.Tags(), function (t) {
                return t.projectID === project.projectID;
            })
        }
    });

    self.getTags = function (data) {
        return ko.computed(function () {
            return ko.utils.arrayFilter(self.projectTags(), function (t) {
                return t.textID === data.textID && !t._destroy;
            });
        });
    };

    // remove tag
    self.removeTag = function (tag) {
        self.Tags.destroy(tag);
        self.Tags.remove(tag); // I´ve tried to remove...
        self.Tags.push(tag);   // … and push the 'tag' back to trigger the computed.
    };
};

然后我的观点:

<div data-bind="with: $root.selectedText">
    <div class="tag-list" data-bind="sortable:{template:'tagsTmpl',data:$root.getTags($data)}"></div>
</div>

<script type="text/html" id="tagsTmpl"><div>
    <span class="tag-action remove" data-bind="click: function(){$root.removeTag($data)};">x</span>
    <span class="tag-text" data-bind="text: text"></span>  
</div></script>

0 个答案:

没有答案