gpt4 book ai didi

javascript - knockout : Why is there an empty object sent along?

转载 作者:行者123 更新时间:2023-11-28 10:03:27 25 4
gpt4 key购买 nike

我正在尝试使用knockoutjs 编写rails3 crud View 。不知何故,我得到的行为与教程完全不同,如果您能给我一些提示,我将不胜感激......

我的代码更多的是对 loading and saving data tutorial 的复制。 。我从服务器加载数据,什么都不做,保存, Controller 获取额外的“空”对象,该对象以某种方式被持久化。然而,对现有数据的更改永远不会发生,也不会被删除。

我可以显示代码吗:

%ul#folders{'data-bind' => 'foreach: folders, visible: folders().length > 0'}
%li
%span{'data-bind' => 'text: id'}
%span{'data-bind' => 'text: name'}
%span{'data-bind' => 'text: description, visible: description'}
%a{'data-bind' => 'click: $parent.removeFolder'}Excluir

%button{'data-bind' => 'click: save'}Salvar

%form{'data-bind' => 'submit: addFolder'}
Add folder:
%input{'data-bind' => 'value: newFolderName', 'placeholder'=>"Nome"}
%button{type: "submit"}Adicionar

=content_for :script do
:javascript

function Folder(data) {
data = data || {id: 0, name: "", description: ""};
this.id = data.id;
this.name = ko.observable(data.name);
this.description = ko.observable(data.description);
}

function FolderListViewModel() {
var self = this;
self.folders = ko.observableArray([]);
self.newFolderName = ko.observable();

$.getJSON("/folders", function(allData) {
var mappedFolders = $.map(allData, function(item) {
return new Folder(item);
});
self.folders(mappedFolders);
});

self.addFolder = function() {
self.folders.push(new Folder({name: self.newFolderName()}));
self.newFolderName("");
};

self.removeFolder = function(folder) {
self.folders.remove(folder);
};

self.save = function() {
console.log(self.folders());
$.ajax("/folders", {
data: ko.toJSON({folders: self.folders}),
type: "post",
contentType: "application/json",
success: function(result) {
return console.log(result);
}
});
};
}

$(function(){
ko.applyBindings(new FolderListViewModel());
});

保存(无论是否有更改都是独立的)总是会产生类似的请求:

Started POST "/folders" for 127.0.0.1 at 2012-01-04 22:57:40 +0100
Processing by FoldersController#create as */*
Parameters: {"folders"=>[{"id"=>2, "name"=>"dim sum", "description"=>"ist warm!"}, {"id"=>3, "name"=>"Sushi", "description"=>"roh und kalt"}, {"id"=>1, "name"=>"Spagetti", "description"=>"Carbonara"}, {"id"=>10, "name"=>"Wiener", "description"=>"vom Kalb"}, {"id"=>11, "name"=>"Feijão", "description"=>""}],
"folder"=>{}} <== LOOK HERE
Folder Load (0.3ms) SELECT "folders".* FROM "folders" ORDER BY "folders"."row_order"
(0.1ms) BEGIN
Folder Load (0.2ms) SELECT id, row_order FROM "folders" ORDER BY "folders"."row_order" DESC LIMIT 1
Folder Load (0.2ms) SELECT id, row_order FROM "folders" WHERE "folders"."row_order" = 8388592 LIMIT 1
SQL (0.4ms) INSERT INTO "folders" ("created_at", "description", "name", "row_order", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Wed, 04 Jan 2012 21:57:40 UTC +00:00], ["description", nil], ["name", nil], ["row_order", 8388592], ["updated_at", Wed, 04 Jan 2012 21:57:40 UTC +00:00]]
(0.5ms) COMMIT
Redirected to http://0.0.0.0:3000/folders/21
Completed 302 Found in 54ms

此外,删除的对象仅从数组中删除,我想知道 Rails 应该如何弄清楚,它应该将它们从对象存储中删除...一切都只是一个帖子...这对我来说看起来都是错误的。我的错误是什么?

两个奖励问题:我不喜欢用这些构造函数来填充我的全局 js 命名空间,但如果我尝试将它们隐藏在匿名函数中,似乎 ko 再也找不到它们了。这个问题可以补救吗?

我怎样才能在全局范围内告诉rails,停止将我重定向到json请求上的html操作(无论如何这有多愚蠢)并且只向我发送一条ok消息或错误消息?

最佳答案

对于全局变量,您可以通过为它们创建命名空间和/或将此页面的代码包装在代 TableView 模型的函数/对象中来解决这个问题。例如:

var 我=我的|| {};//你的命名空间。可能会在整个应用程序的一个地方定义它。

然后您可以像平常一样定义 View 模型,甚至将其包装在自调用函数中(如下所示)。我强烈推荐 View 模型方法,因此页面的所有行为都卡在一个对象上。

(function (my, ko) {
"use strict";
my.viewmodel = {
firstName: ko.observable(),
lastName: ko.observable()
};
}(my, ko));

关于javascript - knockout : Why is there an empty object sent along?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8735004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com