cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
var Model = {
	inherited: function(){
   //console.dir(this);
  },
	created: function(){
   // console.dir(this);
  },
	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;
	},
  extend: function(o){
		var extended = o.extended;
    	for(i in o){
        this[i]=o[i];
      }
		if (extended) extended(this);
	},
	include: function(o){
		var included = o.included;
			for(i in o){
        this.prototype[i]=o[i];
      }
		if (included) included(this);
	}
};
//自动生成ID方法
Math.guid = function(){
	return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
	var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
	return v.toString(16);
	}).toUpperCase();
};

Model.extend({
created: function(){
	this.records = {}; 
   this.attributes=[];
}
});

//向ORM 中添加记录
Model.extend({
  populate:function(datas){
    this.records={};
    for(var i=0,len=datas.length;i<len;i++){
      var record=this.init(datas[i]);
      record.newRecord=false;
      this.records[record.id]=record;
    }
  }
});

Model.extend({
  // 通过ID查找
  find: function(id){
    var record=this.records[id];  
    return record.dup();
  }
});

Model.include({
   init: function(atts) {
		if (atts) this.load(atts);
	},
	load: function(attributes){
	for(var name in attributes)
	this[name] = attributes[name];
	}
});
Model.include({
   newRecord: true,
   create:function(){
     this.newRecord=false;
     if(!this.id)this.id=Math.guid();
     this.parent.records[this.id]=this.dup();
  },
  update:function(){
    this.parent.records[this.id]=this.dup();
  },
  destroy:function(){
    delete this.parent.records[this.id];
  },
  save:function(){
    this.newRecord?this.create():this.update();
  },
  dup:function(){
    return $.extend(true,{},this);
  }
  
});
Model.include({
  attributes:function(){
   	var result={};
    for(var i in this.parent.attributes){
      var attr=this.parent.attributes[i];
      result[attr]=this[attr];
    }
    result.id=this.id;
    return result;
  },
  toJSON:function(){
    return this.attributes();
  }
})
var LocalStorage={
  saveLocal:function(name){
    var result=[];
    for(var i in this.records){
      result.push(this.records[i]);
    }
    localStorage[name]=JSON.stringify(result);
  },
  loadLocal:function(name){
    var result = JSON.parse(localStorage[name]);
    this.populate(result);
  }
};
Model.extend(LocalStorage);
Model.include({
createRemote: function(url, callback){
$.post(url, this.attributes(), callback);
},
updateRemote: function(url, callback){
$.ajax({
url: url,
data: this.attributes(),
success: callback,
type: "PUT"
});
}
});

//拷贝Model
var User=Model.create();
//console.log(User);
User.attributes=['id','name'];
var user1=User.init({id:1,name:"heyong"});
user1.save();

var user2=User.init({id:2,name:"lilei"});
user2.save();

var f_user1=User.find(1);
f_user1.name="22";
user2.name="33";
/*console.log(User);
*/
var Asset=Model.create();
Asset.attributes=['id','title'];

var asset1=Asset.init({id:1,title:"1111标题唉唉唉"});

var asset2=Asset.init({id:2,title:"2222标题唉唉唉"});
asset1.save();
asset2.save();
asset2.createRemote("/asset");

Asset.saveLocal("Asset");
Asset.loadLocal("Asset");
console.log(Asset);

var attr=asset1.attributes();
console.log(attr);
var json = JSON.stringify(Asset.records);
console.log(json);


localStorage.setItem("someData", "wem");
console.log(localStorage.getItem("someData"));
//本地存储API



            
          
!
999px
Loading ..................

Console