好久没有写新文章啦。最近比较喜欢用印象笔记,因此一些碎碎的知识自己都用印象笔记做了,这篇文章主要是解决我遇到的一些坑,以及针对这些坑我在网上查到的解决方案
用backbone.js删除数据的时候没有向服务器发送delete请求:
解决方案:增加id属性,见
http://blog.csdn.net/wuyajun1124/article/details/41622867
用mongoose 插入数据;然后用mongodb shell 查看插入的数据,查不出来
这个算是挺基础的问题了,解决方案:https://segmentfault.com/q/1010000005901209
另外自己再附上两篇mongoose的文章:
http://cnodejs.org/topic/504b4924e2b84515770103dd
http://www.nonb.cn/blog/nodejs-mongoose-query-chinaese.html
文档:http://www.nodeclass.com/api/mongoose.html#guide_queries
backbone有一个validate机制,这个函数比较特别
关于这个函数的说明在:http://www.cnblogs.com/snandy/p/3257743.html
另外自己也写了一段代码来展示这个机制在表单验证的时候起到的作用(有被优化的空间):
<!doctype html>
<html>
<head>
<meta charset=utf-8>
<title>Form Validation - Model#validate</title>
<script src='http://code.jquery.com/jquery.js'></script>
<script src='http://underscorejs.org/underscore.js'></script>
<script src='http://backbonejs.org/backbone.js'></script>
<script>
jQuery(function($) {
// alert('haha');
var User = Backbone.Model.extend({
initialize:function() {
this.on('change',function(){
alert('change');
console.log('change');
})
},
validate: function(attrs) {
var errors = this.errors = {};
if (!attrs.firstname) errors.firstname = 'firstname is required';
if (!attrs.lastname) errors.lastname = 'lastname is required';
if (!attrs.email) errors.email = 'email is required';
if (!_.isEmpty(errors)) return errors;
},
haha:function(){
alert('haha');
}
});
var Field = Backbone.View.extend({
events: {blur: 'validate'},
initialize: function() {
this.name = this.$el.attr('name');
this.$msg = $('[data-msg=' + this.name + ']');
},
validate: function() {
this.model.set(this.name, this.$el.val(), {validate:true,haha:true});
this.$msg.text(this.model.errors[this.name] || '');
}
});
//向model设置一个或多个hash属性(attributes)。如果任何一个属性改变了model的状态,在不传入 {silent: true} 选项参数的情况下,会触发 "change" 事件,更改特定属性的事件也会触发。
var user = new User;
$('input').each(function() {
new Field({el: this, model: user});
});
});
</script>
</head>
<body>
this is a sample
<form>
<label>First Name</label>
<input name='firstname'>
<span data-msg='firstname'></span>
<br>
<label>Last Name</label>
<input name='lastname'>
<span data-msg='lastname'></span>
<br>
<label>Email</label>
<input name='email'>
<span data-msg='email'></span>
</form>
</body>
</html>
如何对backbone的collection进行过滤
这一个地方自己以前不会,这里给出一篇文章参考:
http://www.w3cboy.com/post/2014/09/如何对Backbone.Collection进行过滤操作/
另外自己的github上面有两个todos的源码例子,其中有涉及到用路由进行过滤的方法,可以常看看。