当前位置: 首页 > 编程日记 > 正文

mvc-3模型和数据(1)

MVC和命名空间

var User = function(atts) {this.attribute = atts || {};
}
//和具体user相关的方法
User.prototype.destroy = function() {};
//和具体user不相关的函数和变量
User.fetchRemove = function() {};
var user = new User({name:'jinks'});
user.destroy();

构建对象关系映射

如:

  • 任何model的改变会向后台发起一个ajax请求
  • model和view绑定,当一个实例改变时,马上从view中反映出来

原型继承

  • 这里用到Object.create,对于没有的浏览器,可以模拟方法
if(typeof Object.create !== "function") {Object.create = function(o) {function F(){};F.prototype = o;return new F();}
}
var Model = {inherited: function() {},created: function() {},prototype: {init: function() {}},create: function() {var object = Object.create(this);object.parent = this;object.prototype = object.fn = Object.create(this.prototype);object.created();this.inherited(object);return object;},init: function() {var instance = Object.create(this.prototype);instance.parent = this;instance.init.apply(instance, arguments);return instance;}
}
var Asset = Model.create();
var User = Model.create();
var user = User.init();

添加ORM属性

var Model = {/*代码片段*/extend: function(o) {var extended = o.extended;for(var i in o) {this[i] = o[i];}if(extended) extended(this);},include: function(o) {var included = o.included;for(var i in o) {this.prototype[i] = o[i];}if(included) included(this);}
}
//添加到Model对象属性
Model.extend({find: function() {}
});
//添加Model.prototype中,所有实例共享这一属性
Model.include({load: function(atts) {for(var i in atts) {this[i] = atts[i];}},init: function(atts) {if(atts) this.load(atts);}
});var Asset = Model.create();
var asset = Asset.init({name: "a.png"});

持久化记录

将引用保存至新创建的实例中以便任何时候都可以访问

var Model = {/*代码*/
}
//用来保存资源的对象
Model.records = {};
Model.include({newRecord: true,create: function() {this.newRecord = false;this.parent.records[this.id] = this;},destroy: function() {delete this.parent.records[this.id];}
});
//更新一个已经存在的实例
Model.include({update: function() {this.parent.records[this.id] = this;}
});
//添加一个快捷函数来保持实例
Model.include({save: function() {this.newRecord ? this.create() : this.update();}
});
Model.extend({//通过id查找,找不到抛出异常find: function(id) {var id = this.records[id];if(id) {return id;} else {throw new Error("Unknow record");}}
});var Asset = Model.create();
var asset = Asset.init();
asset.name = "same, same";
asset.id = 1;
asset.save();var asset2 = Asset.init();
asset2.name = "but different";
asset2.id = 2;
asset2.save();console.log(Asset.records);
asset2.destroy();
console.log(Asset.records);

增加ID支持

使用全局统一标识(GUID)生成器来自动生成ID

//使用Math.random()来产生一个伪随机数的GUID
Math.guid = function(){return 'xxxxxxxx-xxxx-4xxx-yxxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c){var r = Math.random() * 16 | 0,v = c === 'x' ? r : (r & 0x3 | 0x8);return v.toString(16)}).toUpperCase();
};
//修改create()函数
Model.include({create: function() {if(!this.id) this.id = Math.guid();this.newRecord = false;this.parent.records[this.id] = this;}
});var Asset = Model.create();
var asset = Asset.init();
asset.save();
console.log(asset.id);

转载于:https://www.cnblogs.com/jinkspeng/p/4226292.html

相关文章:

初步了解:使用JavaScript进行表达式(De Do Do Do,De Da Da Da)

by Donavon West由Donavon West 初步了解:使用JavaScript进行表达式(De Do Do Do,De Da Da Da) (A first look: do expressions in JavaScript (De Do Do Do, De Da Da Da)) This article is not about about the The Police’s 1980 hit song from alb…

div 下 的img水平居中

设置text-align:center; 这个div必须要设置宽度; 如:{text-align:center; width:100%;}转载于:https://www.cnblogs.com/zzd0916/p/6626772.html

Understanding SOAP

Understanding SOAP转载于:https://www.cnblogs.com/daishuguang/p/4227983.html

js删除组数中的某一个元素(完整代码附效果图)

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; <view class"big-logos"> <imagebindtap"addimg"src../../../image/s.png></image> <blockwx:for"{{img_arr}}"wx:key"in…

c专家编程/c陷阱_如何避免常见的初学者陷阱并像专家一样开始编码

c专家编程/c陷阱by Dmitri Grabov德米特里格拉波夫(Dmitri Grabov) 如何避免常见的初学者陷阱并像专家一样开始编码 (How to avoid common beginner pitfalls and start coding like a pro) Learning to code is tough. We’ve all encountered cryptic errors and code break…

xmpp关于后台挂起的消息接收,后台消息推送,本地发送通知

想问下&#xff0c;在xmpp即时通讯的项目中&#xff0c;我程序如果挂起了&#xff0c;后台有消息过来&#xff0c;我这边的推送不过来&#xff0c;所以我的通知就会收不到消息&#xff0c;当我重新唤醒应用的时候&#xff0c;他才会接收到通知&#xff0c;消息就会推送过来&…

[冲昏头脑]IDEA中的maven项目中学习log4j的日志操作

第一&#xff0c;你要有log4j的对应的包&#xff0c;由于我用的maven&#xff0c;所以直接在pom.xml文件依赖下载则可&#xff0c;如你尚为有此包&#xff0c;请自行百度下载导入&#xff0c;或上http://www.mvnrepository.com/搜索。上如则是我的log4j的包的版本。好了&#x…

女神推荐, 卡片,广告图 ,点击查看更多

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; <view><view classtitle>女神推荐 </view> <image stylemargin-top:25rpx; classtab_img src{{img list_data.q1[1].image}}></image><view classv…

aws lambda_恐怕您正在考虑AWS Lambda的冷启动完全错误

aws lambdaby Yan Cui崔燕 恐怕您正在考虑AWS Lambda的冷启动完全错误 (I’m afraid you’re thinking about AWS Lambda cold starts all wrong) When I discuss AWS Lambda cold starts with folks in the context of API Gateway, I often get responses along the line of…

python tkinter窗口弹出置顶的方法

加上下面两句即可实现root窗口的置顶显示&#xff0c;可以用于某些程序的消息提示&#xff0c;能够弹出到桌面显示 root Tk() root.wm_attributes(-topmost,1) 转载于:https://www.cnblogs.com/shuchengxiang/p/6632140.html

用Quartus II Timequest Timing Analyzer进行时序分析 :实例讲解 (一)

一&#xff0c;概述 用Altera的话来讲&#xff0c;timequest timing analyzer是一个功能强大的&#xff0c;ASIC-style的时序分析工具。采用工业标准--SDC&#xff08;synopsys design contraints&#xff09;--的约束、分析和报告方法来验证你的设计是否满足时序设计的要求。在…

软件工程与软件测试基础知识_这是我在软件工程工作九个月中学到的知识

软件工程与软件测试基础知识I’ve been working for about nine months at Dexter as a software developer. I wrote a blog post about landing the job initially, as well as a technical post about a self positioning component I made in my first couple of months at…

[bzoj1064][Noi2008]假面舞会

题意:有n个人&#xff0c;每个人都有一个标号&#xff0c;每种标号的人只能看见下一个标号的人&#xff08;最后一种标号看见第一种&#xff09;&#xff0c;给定m个关系&#xff0c;求这个关系是否合法以及合法情况下最大和最小的方案数。$n\leqslant 10^{5},m\leqslant 10^{6…

js取一定范围内的随机整数

Math.floor(Math.random()*305 1); Math.random()*305 的作用是取0到305的随机数 加1 就是1到305的随机数, Math.floor是取整数, 所以最后的结果是0到305的随机整数 微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。

AE,按照属性值关系选择要素

if(axMapControl2.LayerCount<0) { MessageBox.Show("请加载图层后使用该功能","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning); } else { ILayer pLayer axMapControl2.get_Layer(0); IFeatureLayer pFeatureLayer pLayer as IFeatureLay…

aws lambda使用_如何使用AWS Lambda和S3构建无服务器URL缩短器

aws lambda使用by Daniel Ireson丹尼尔埃里森(Daniel Ireson) 如何使用AWS Lambda和S3构建无服务器URL缩短器 (How to build a Serverless URL shortener using AWS Lambda and S3) Throughout this post we’ll be building a serverless URL shortener using Amazon Web Ser…

android -volley-请求数据

private List<gson.DataBean>arrGson;//请求的数据 //请求数据的方法 public void initData() { RequestQueue mQueue Volley.newRequestQueue(getApplicationContext()); String url "http://www.fashions88.com/you/HBooks88/GetBooks88Data…

[微信小程序]动画,从顶部掉花的效果(完整代码附效果图)

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; image{ width: 100rpx;height: 100rpx;position: absolute;top: -100rpx; } 如果对您有帮助&#xff0c;请关注我&#xff0c;欢迎加入微信小程序开发交流QQ群&#xff08;173683866&…

pl/sql developer连接远程数据库

本地不安装oracle client程序&#xff0c;直接使用pl/sql developer连接远程数据库 考虑到机子本身资源有限&#xff0c;一个client会占用很多资源&#xff0c;尝试使用不安装客户端的方式进行远程连接。 需要软件&#xff1a;instantclient-basic-win32-10.2.0.5.zip、pl/sql …

工厂用抽象类比接口_用简单的现实类比解释硬编码概念

工厂用抽象类比接口by Samer Buna通过Samer Buna 用简单的现实类比解释硬编码概念 (Hard Coding Concepts Explained with Simple Real-life Analogies) 如何向5岁的孩子解释诸如流&#xff0c;promise&#xff0c;lint和声明性编程之类的编码概念 (How to explain coding con…

[微信小程序]点击切换卡片动画效果

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先上效果图, GIF: <!--pages/roll/roll.wxml--> <!-- 单身 --> <block wx:if"{{danshen}}"><view class"card_wrap"><view animatio…

redis学习之——Redis事务(transactions)

Redis事务&#xff1a;可以一次执行多个命令&#xff0c;本质是一组命令的集合。一个事务中的&#xff0c;所有命令都会序列化&#xff0c;按顺序地串行化执行而不会被其它命令插入&#xff0c;不许加塞。 常用命令&#xff1a;MULTI 开启事务 EXEC 提交事务、 DISCARD 放弃…

WordPress页面Page和文章Post的相互转换

1. 进入phpMyAdmin&#xff1b; 2. 进入WordPress对应的数据库&#xff1b; 3. 浏览wp_posts数据表&#xff1b; 4. 找到相应的 页面Page 并编辑&#xff08;找到相应的 文章Post 并编辑&#xff09;&#xff1b; 5. 修改 post_type 为 post&#xff08;page&#xff09;&#…

airbnb_我如何在一个晚上建立音乐工作室的Airbnb

airbnbby Mike Williams由Mike Williams 我如何在一个晚上建立音乐工作室的Airbnb (How I built the Airbnb of music studios in a single evening) Sometimes you come up with an idea that you just know has to be built. That was the case I came up with Studiotime, …

QT学习第8课:QT计算器界面实现

声明&#xff1a;此文章仅是个人在学习狄泰QT课程所做的笔记&#xff0c;文章中包含狄泰资料的&#xff0c;一切版权归狄泰软件所有&#xff01;   第8课是来做一个计算器界面&#xff0c;只是一个界面显示。不过也是挺兴奋的&#xff0c;以前一直对着黑框框&#xff0c;现在…

C# 实现对接电信交费易自动缴费 续(winio/winring0 自动填密码)

自动填密码大家可能都不莫生,最有名的应该是 按键精灵 只要是一个可以输入的地方都可以能过按键精灵来完成输入.我今天要讲的是使用 winio/winring0来完成类似的功能 如果要自动填充密码方式基本上有 消息级的模拟 和 驱动级的模拟, 消息级的模拟如 C# 直接使用 SendKeys 就可以…

[微信小程序]js动态改变数组对象列表中的样式

微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文&#xff1a; 这里我用微信小程序商城开发中选择商品规格选择做示例: 先把效果图让大家看看, 默认情况下是这样的 当点击了规格11以后: 该商品规格的颜色变成红色,并且显示该规格商品的图片和…

ios snapkit m_如何使用自动布局和SnapKit在iOS上创建漂亮的拉伸布局

ios snapkit mby Enabled Solutions由Enabled Solutions 如何使用自动布局和SnapKit在iOS上创建漂亮的拉伸布局 (How to create beautiful Stretchy Layouts on iOS using Auto Layout and SnapKit) Check the image below. This is a cool effect.检查下面的图像。 这是一个很…

谷歌 notification 测试 页面

1 <button onclick"notifyMe(master wei,http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png,我在测试谷歌通知功能,http://www.mi.com/)">通知!</button>2 <script>3 var sum 0;4 document.addEventListener(DOMContentLoaded, fun…

[转]如果我有jQuery背景,我应该如何切换到AngularJS的思维模式?

导言 stackoverflow上有一个人问了一个问题&#xff1a;如果我有jQuery背景&#xff0c;我应该如何切换到AngularJS的思维模式&#xff1f; 有一个回复非常经典&#xff0c;获得了两千多票。 为了让国内开发者也能领略到其中的核心思想&#xff0c;现把这个问题和答案翻译出来供…