【问题标题】:Storing values from json object in database - knockout.js在数据库中存储来自 json 对象的值 - knockout.js
【发布时间】:2014-03-31 14:51:07
【问题描述】:

我目前正在学习 knockout.js 及其独特功能。我已经成功创建了一个联系表。我可以根据需要添加或删除尽可能多的联系人。我不太了解将值存储在数据库中的概念。我将值放在 JSON 对象中,然后使用 $.post("/some/url.php" 发送值以存储在 mysql db 中。但是,根本不工作。我在服务器端使用 php。如何将联系人值存储在 mysql db 中?我还需要将json对象解码为php中的普通数组进行存储吗? JSFIDDLE

var initialData = [{
    firstName: "Jenny",
    lastName: "LaRusso",
    phone: "(555) 121-2121",
    alt_phone: "(555) 123-4567",
    main1: false,
    main2: true    
}, {
    firstName: "Sensei",
    lastName: "Miyagi",
    phone: "(555) 444-2222",
    alt_phone: "(555) 999-1212",
    main1: true,
    main2: false
}];

var ContactsModel = function (contacts) {
    var self = this;
    self.contacts = ko.observableArray([]);

    ko.utils.arrayForEach(contacts, function (contact) {
        self.contacts.push({
            firstName: contact.firstName,
            lastName: contact.lastName,
            phone: contact.phone,
            alt_phone: contact.alt_phone,
            main1: ko.observable(contact.main1),
            main2: ko.observable(contact.main2)
        });
    });

    self.addContact = function () {
        self.contacts.push({
            firstName: "",
            lastName: "",
            phone: "",
            alt_phone: "",
            main1: false,
            main2: false
        });
    };

    self.removeContact = function (contact) {
        self.contacts.remove(contact);
    };

    self.addPhone = function (contact) {
        contact.phones.push({
            number: ""
        });
    };

    self.removePhone = function (phone) {
        $.each(self.contacts(), function () {
            this.phones.remove(phone)
        })
    };

    self.save = function () {
            self.lastSavedJson(JSON.stringify(ko.toJS(self.contacts), null, 2));
        };


    self.lastSavedJson = ko.observable("");

    //This is not working
    $.post("/some/url.php", initialData, function(returnedData) {
        // This callback is executed if the post was successful   
    })
};

ko.applyBindings(new ContactsModel(initialData));

【问题讨论】:

    标签: javascript php json knockout.js


    【解决方案1】:

    您的小提琴不起作用,因为它没有引用 jQuery。也许那是你的问题。这是一个固定的小提琴:http://jsfiddle.net/azurelogic/dLbY7/17/。现在我只得到一个 404,因为“/some/url.php”不是真实的。

    编辑:

    逻辑也关闭了。您应该在保存函数之前声明 lastSavedJson。此外,需要在保存中调用帖子。像这样:

    self.lastSavedJson = ko.observable("");
    
    self.save = function () {
        self.lastSavedJson(JSON.stringify(ko.toJS(self.contacts), null, 2));
        $.post("/some/url.php", self.lastSavedJson(), function(returnedData) {
            // This callback is executed if the post was successful   
        })
    };
    
    //initial post if it is still needed
    $.post("/some/url.php", initialData, function(returnedData) {
        // This callback is executed if the post was successful   
    })
    

    如果您实际上不需要将 lastSavedJson 用于其他任何事情,您可以将其内联到帖子中。

    我发现如果我先声明所有的 observables,然后是计算的,最后是函数,这会有所帮助。

    【讨论】:

    • 好的,有道理。但是这个帖子的逻辑是否正确。例如,如果单击save 按钮,我想发布
    • 编辑了解决逻辑问题的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2021-01-29
    • 2010-10-15
    • 2012-07-08
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多