我经常使用Knockout,而且经常需要在data-bind属性中编写脚本。我可以在这些标记文件上使用任何验证工具来验证File "load_data.py", line 10, in <module>
"VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", row)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ription, TransactionState, TransactionType, CategoryID, AcctCategoryID, CheckNum' at line 1")
属性中的javascript吗?如果有一个咕噜的插件会很好。
答案 0 :(得分:3)
可能没有(一个突出的),因为在你的视图中有很多复杂的逻辑并不常见。使用类似MVVM的方法,如果你保持View相当简单,并在ViewModel中写出可以对其进行单元测试的逻辑,那么它最有效。
所以执行不执行此操作:
var ViewModel = function() {
var self = this;
self.isCurrent = ko.observable(false);
self.item = ko.observable({ id: 42 });
}
ko.applyBindings(new ViewModel());
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<!-- Don't do this! -->
<div data-bind="visible: !isCurrent() && !!item()">
Showing something!
</div>
&#13;
相反, :
var ViewModel = function() {
var self = this;
self.isCurrent = ko.observable(false);
self.item = ko.observable({ id: 42 });
self.shouldShowItem = ko.computed(function() {
return !self.isCurrent() && !!self.item();
});
}
ko.applyBindings(new ViewModel());
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<!-- Don't do this! -->
<div data-bind="visible: shouldShowItem">
Showing something!
</div>
&#13;
因为这样可以对shouldShowItem
逻辑进行单元测试,例如使用QUnit:
QUnit.test("shouldShowItem is false if not isCurrent and item not set", function(assert) {
var vm = new ViewModel();
vm.isCurrent(false);
vm.item(null);
assert.strictEqual(vm.shouldShowItem(), false);
});
最重要的是,如果您发现自己在视图中编写了大量逻辑,则可能需要将其中一部分移动到视图模型中并使其可测试。