KnockOutJS触发子订阅的父函数

时间:2013-03-20 18:15:24

标签: javascript mvvm knockout.js

我目前正在尝试学习KnockOutJS。我认为创建一个简单的任务列表应用程序是个好主意。 我不想在这里写一篇长文,让我们深入研究我的问题。我很感激所有的帮助 - 我是KnockOutJS的新手!

任务声明如下:

var Task = function (data) {
    var self = this;

    self.name = ko.observable(data.name);
    self.status = ko.observable(data.status);
    self.priority = ko.observable(data.priority);
}

视图模型看起来像这样

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

self.currentTask = ko.observable();
self.currentTask(new Task({ name: "", status: false, priority: new Priority({ name: "", value: 0 }) }));

self.tasksArr = ko.observableArray();

self.tasks = ko.computed(function () {
    return self.tasksArr.slice().sort(self.sortTasks);
}, self);

self.sortTasks = function (l, r) {
    if (l.status() != r.status()) {
        if (l.status()) return 1;
        else return -1;
    }

    return (l.priority().value > r.priority().value) ? 1 : -1;
};

self.priorities = [
    new Priority({ name: "Low", value: 3 }),
    new Priority({ name: "Medium", value: 2 }),
    new Priority({ name: "High", value: 1 })
];

// Adds a task to the list
// also saves updated task list to localstorage
self.addTask = function () {
    self.tasksArr.push(new Task({ name: self.currentTask().name(), status: false, priority: self.currentTask().priority() }));
    self.localStorageSave();

    self.currentTask().name("");
};

// Removes a task to a list
// also saves updated task list to localstorage
self.removeTask = function (task) {
    self.tasksArr.remove(task);
    self.localStorageSave();
};

// Simple test function to check if event is fired.
self.testFunction = function (task) {
    console.log("Test function called");
};

// Saves all tasks to localStorage
self.localStorageSave = function () {
    localStorage.setItem("romaTasks", ko.toJSON(self.tasksArr));
};

// loads saved data from localstorage and parses them correctly.
self.localStorageLoad = function () {
    var parsed = JSON.parse(localStorage.getItem("romaTasks"));
    if (parsed != null) {
        var tTask = null;
        for (var i = 0; i < parsed.length; i++) {
            tTask = new Task({
                name: parsed[i].name,
                status: parsed[i].status,
                priority: new Priority({
                    name: parsed[i].priority.name,
                    value: parsed[i].priority.value
                })
            });
            self.tasksArr.push(tTask);
        }
    }
};
self.localStorageLoad();
}

我想在html中做的事情非常简单。 我添加的所有任务都保存到localStorage。正如您所看到的,每次添加元素时都会调用保存功能。除去。但是我也想在每个任务的状态发生变化后立即保存,但是这里不能使用subscribe,例如

self.status.subscribe(function() {});

因为我无法从Task类访问self.tasksArr。

有什么想法吗?有可能以某种方式使self.tasksArr公开吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

self.addTask = function () {
  var myTask = new Task({ name: self.currentTask().name(), status: false, priority: self.currentTask().priority() })
  myTask.status.subscribe(function (newValue) {
    self.localStorageSave();
  });
  self.tasksArr.push(myTask);
  self.localStorageSave();
  self.currentTask().name("");
};