在基于Knockoutjs和Sammy.js的Web应用程序中,我有三个以父 - 子方式相互依赖的可观察对象(第二个是第一个孩子,第三个是第二个孩子) 。我的HTML有三个部分,一次只能看到一个部分。每个部分都依赖于使用可见绑定提到的其中一个观察对象。在knockoutjs中嵌套的observables
我的URL方案的布局类似于/#id-of-parent/id-of-child/id-of-grandchild(在Sammy.js中)。
如果我访问一个完整的URL(其中一个带有全部三个ID),我就会对这些观察值产生麻烦。在Sammy规则函数中,我首先加载并存储父项,然后是子项,最后是孙子(这实际上是用户想要查看的数据)。问题是父母和孩子的绑定也被触发。
有没有办法避免触发绑定或有没有更好的方法来组织这样的应用程序?
这里是我的萨米路线:
Sammy(function() {
this.get('#study/:id', function() {
self.studylist(null);
self.currentStudy(Study.loadById(this.params.id));
});
this.get('#study/:id/variableGroups', function() {
self.variableGroupList(self.currentStudy().variableGroups());
self.currentVariable(null);
});
this.get('#study/:id/variable-group/:variableGroup/variables', function() {
var groupId = this.params.variableGroup;
$.ajax(apiUrl + "/variable-group/" + groupId, {
type: "GET",
async: false,
cache: false,
context: this,
success: function(data) {
if (!self.currentStudy()) {
self.currentStudy(Study.loadById(this.params.id));
}
self.currentVariableGroup(new VariableGroup(data.variablegroup));
self.variableList(self.currentVariableGroup().variables);
}
});
});
this.get('#study/:id/:variableGroupId/:variableId', function() {
var variableId = this.params.variableId;
$.ajax(apiUrl + "/variable/" + variableId, {
type: "GET",
async: false,
cache: false,
context: this,
success: function(data) {
if (!self.currentStudy()) {
self.currentStudy(Study.loadById(this.params.id));
}
if (!self.currentVariableGroup()) {
self.currentVariableGroup(VariableGroup.loadById(this.params.variableGroupId));
}
self.currentVariable(new Variable(data.variable));
}
});
});
this.get("", function() {
self.currentStudy(null);
self.currentVariableGroup(null);
self.currentVariable(null);
$.get(apiUrl + "/study/all", function(data) {
var mappedStudies = $.map(data.studies, function(item, index) {
return new Study(item);
});
self.studylist(mappedStudies);
});
});
this.get('', function() { this.app.runRoute('get', "")});
}).run();
您可以发布您的Sammy代码。 – Tyrsius 2012-07-23 15:28:31
已添加Sammy代码。 – fiskeben 2012-07-24 08:21:58
因此,为了澄清,如果你想要设置currentStudy,currentVariableGroup和currentVariable,但你不希望任何订阅者(比如html绑定或者可观测的)从前两个更新? – Tyrsius 2012-07-24 16:03:09