我已經創建使用Rivets.js和Watch.JS演示,
http://jsfiddle.net/nsisodiya/njDGA/
```
var FlatKey = {
set: function (Obj, keypath, value) {
//console.log("set : " + keypath + " with " + value);
var V = Obj;
var keyArry = keypath.split(".");
keyArry.filter(function (v, i, A) {
return i !== A.length - 1;
}).map(function (v) {
if (V[v] === undefined) {
V[v] = {};
}
return V = V[v];
});
V[keyArry.pop()] = value;
},
get: function (Obj, keypath) {
var V = Obj;
keypath.split(".").map(function (v) {
if (V[v] === undefined) {
return V = "";
}
return V = V[v];
});
return V;
},
findsubPath: function (str) {
return str.split(".").filter(function (v, i, A) {
return i !== A.length - 1;
}).join(".");
}
};
rivets.configure({
adapter: {
subscribe: function (obj, keypath, callback) {
var subpath = FlatKey.findsubPath(keypath);
//console.log("subscribed : " + keypath + " : subpath = " + subpath);
if (subpath === "") {
watch(obj, keypath, callback);
} else {
watch(FlatKey.get(obj, subpath), keypath.split(".").pop(), callback);
}
},
unsubscribe: function (obj, keypath, callback) {
//console.log("unsubscribed : " + keypath);
var subpath = FlatKey.findsubPath(keypath);
if (subpath === "") {
unwatch(obj, subpath, callback);
} else {
unwatch(FlatKey.get(obj, subpath), keypath.split(".").pop(), callback);
}
},
read: function (obj, keypath) {
//console.log("read : " + keypath + " is " + FlatKey.get(obj, keypath));
return FlatKey.get(obj, keypath);
},
publish: function (obj, keypath, value) {
FlatKey.set(obj, keypath, value);
//console.log("publish : " + keypath);
}
}
});
``` 它支持深層次的數據綁定! 希望這會很有用!
我想用spine.js根據描述支持使用鉚釘。我不知道我是否理解正確,但在問題中發佈的示例中。我應該至少在#user> p中看到「用戶」文本。我很難找到從中開始的微不足道的例子。 – Macario 2013-05-30 20:29:33
@Macario在這裏查看我的答案,關於使用Rivets.js和Spine.js的幾個選項。 http://stackoverflow.com/a/17181424/99356 – 2013-06-19 01:20:22