<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="../../app-min.css">
</head>
<body>
<script src="../../jquery/jquery-1.10.2.js"></script>
<script src="../underscore.js"></script>
<script src="../backbone.js"></script>
<script>
var Man = Backbone.Model.extend({
url: '/man/',
initialize: function() {
console.log('initialize')
// 初始化时绑定监听
this.bind('change:name', function() {
var name = this.get('name')
console.log('you changed name to ' + name)
})
this.bind('invalid', function(model, error) {
console.log(error)
})
this.bind('error', function(model, error) {
console.log(error)
})
},
defaults: {
name: 'name',
age: 'age'
},
validate: function(attributes) {
if (attributes.name == '') {
return 'name不能为空'
}
},
aboutMe: function() {
return 'wo jiao ' + this.get('name')
}
}) var man = new Man
console.log(man.get('name'))
man.set({name: ''})
console.log(man.get('name'))
console.log(man.aboutMe())
// 调用save方法时会post对象的所有属性到server端
// 调用fetch方法是又会发送get请求到server端
// 接受数据和发送数据均为json格式
man.save() // save时触发验证。根据验证规则,弹出错误提示 //man.fetch()
//man.fetch({url:'/man/'})
man.fetch({
url: '/man/',
success: function(model, response) {
// model 为获取到的数据
console.log(model.get('name'))
},
error: function() {
console.log('error')
}
})
// 你设置了urlRoot之后,你发送PUT和DELETE请求的时候,其请求的url地址就是:/baseurl/[model.id]
</script>
<article class="content">
<pre>var Man = Backbone.Model.extend({
url: '/man/',
initialize: function() {
console.log('initialize')
// 初始化时绑定监听
this.bind('change:name', function() {
var name = this.get('name')
console.log('you changed name to ' + name)
})
this.bind('invalid', function(model, error) {
console.log(error)
})
this.bind('error', function(model, error) {
console.log(error)
})
},
defaults: {
name: 'name',
age: 'age'
},
validate: function(attributes) {
if (attributes.name == '') {
return 'name不能为空'
}
},
aboutMe: function() {
return 'wo jiao ' + this.get('name')
}
}) var man = new Man
console.log(man.get('name'))
man.set({name: ''})
console.log(man.get('name'))
console.log(man.aboutMe())
// 调用save方法时会post对象的所有属性到server端
// 调用fetch方法是又会发送get请求到server端
// 接受数据和发送数据均为json格式
man.save() // save时触发验证。根据验证规则,弹出错误提示 //man.fetch()
//man.fetch({url:'/man/'})
man.fetch({
url: '/man/',
success: function(model, response) {
// model 为获取到的数据
console.log(model.get('name'))
},
error: function() {
console.log('error')
}
})
// 你设置了urlRoot之后,你发送PUT和DELETE请求的时候,其请求的url地址就是:/baseurl/[model.id]</pre>
</article>
</body>
</html>

-_-#【Backbone】Model的更多相关文章

  1. -_-#【Backbone】Router

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. -_-#【Backbone】View

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. -_-#【Backbone】Collection

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. Linu下的Mysql学习详解_【all】

    Linux下Mysql简介 1.什么是Mysql(MariDB) 1.数据库:存储数据的仓库    2.关系型数据库:mysql(主流用5.5,5.6), oracle        本质:二维表   ...

  5. Mysql 系统学习梳理_【All】

    0.Linux学习---CentOS 7编译安装MySQL 8.0 1.Mysql学习---SQL语言的四大分类 2.Mysql学习---基础操作学习 3.Mysql学习---基础操作学习2 4.My ...

  6. 【Django】--Model字段

    参考地址:http://www.cnblogs.com/wupeiqi/articles/6216618.html 所有字段 AutoField(Field) --int自增列,必须填入参数prima ...

  7. Python 系统学习梳理_【All】

    Python学习 1. Python学习---Python安装与基础1205 2. Python学习---PyCharm的使用学习 3. Python学习---Python数据类型1206 4. Py ...

  8. Linux 下LNMP环境搭建_【all】

    LNMP = Linux + Nginx + Mysql + PHP 1.0 Linux环境搭建 Linux 系统安装[Redhat] 1.1. FastCGI介绍 1.什么是CGI(common g ...

  9. 企业级NFS网络文件共享服务_【all】

    1.1. 什么是NFS(1台机器提供服务) Network File System(网络文件系统)通过局域网让不同的主机系统之间共享文件或目录. NFS客户端可以通过挂载的方式将NFS服务器端共享的数 ...

随机推荐

  1. [io PWA] keynote: Launching a Progressive Web App on Google.com

    Mainly about Material design (effects / colors / flashy stuff) Components (web components / polymer) ...

  2. java 数据库两种连接方法

    package jdbc; import java.sql.*; public class ConnectionDemo2 { public static final String DBDRIVER= ...

  3. [转] __thread关键字

    http://blog.csdn.net/liuxuejiang158blog/article/details/14100897 __thread是GCC内置的线程局部存储设施,存取效率可以和全局变量 ...

  4. codevs 1213 解的个数(我去年打了个表 - -)

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; int T,x ...

  5. webpack 配置 (支持 React SCSS ES6 编译打包 和 模块热更新 / 生成SourceMap)

    1.首先是目录结构 |-node_modules/ #包文件 |-build/ #静态资源生成目录 |-src/ #开发目录 |-js/ |-index.js #入口文件 |-app.js #Reac ...

  6. 关于git的一些常用命令

    1.git init 把目录变成Git可以管理的仓库 2.git add 把文件添加到仓库 3.git commit -m "" 把文件提交到仓库,-m后面是提交说明 4.git ...

  7. log4net 生成多个空文件问题

    使用 log4net 的伙伴,相信很多人会遇到我现在这个问题 ,一般项目需求,便于管理和查找原因,会让项目的日志文件分类记录,然而会出现很多空日志, 出现这个问题的原因通常是我们web.config配 ...

  8. (转)模板引擎类dedetemplate.class.php使用说明

    1.概述 织梦的模板标签类似于XML格式,所有的模板都含有定界符,默认情况下是{dede:*}和{/dede:*},“*”代表模板标记名称. 一般情况下{dede:*}和{/dede:*}是成对出现的 ...

  9. amCharts 破解

    官网下载 http://www.amcharts.com 然后找到amcharts.js , 打开搜索关键字 “utm_source=swf&utm_medium=demo&utm_c ...

  10. 安装php时,make步骤报错make: *** [sapi/fpm/php-fpm] Error 1

    安装PHP过程中,make步骤报错:(集中网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...