我正在尝试使用knockoutjs编写rails3 crud视图。不知怎的,我得到的行为与教程完全不同,如果你能给我一些提示,我会感激不尽......
我的代码不再是loading and saving data tutorial的复制。我从服务器加载数据,什么都不做,保存,控制器获取和另外的“空”对象,它以某种方式持久存在。但是,对现有数据的更改永远不会删除。
我可以展示代码:
%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动作(反正这是多么愚蠢),只是发给我一条确定消息或错误?
答案 0 :(得分:0)
对于全局变量,您可以通过为它们创建命名空间和/或将此页面的代码包装在代表您的viewmodel的函数/对象中来解决这个问题。例如:
var my = my || {}; //你的命名空间可能会在整个应用程序的某个位置定义此内容。
然后您可以像往常一样定义视图模型,甚至将其包装在自调用函数内(如下所示)。我强烈推荐使用viewmodel方法,因此您对页面的所有行为都悬挂在一个对象上。
(function (my, ko) {
"use strict";
my.viewmodel = {
firstName: ko.observable(),
lastName: ko.observable()
};
}(my, ko));