Javascript Mvc学习杂记3

简介:

接着上次说,这次准备在Model类里面,增加本地存储功能,用的是html5中的localStorage,这样方便,页面刷新的时候,自动加载已经添加的数据,下面是静态页的代码。


<!DOCTYPE HTML>
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script src="jquery-1.8.3.js" type="text/javascript"></script>
  <script type="text/javascript" >
     //基于原型的继承
if(typeof Object.create!=="function"){
   Object.create=function(o){
      function F(){}
      F.prototype=o;
      return new F();
   }
}
var Model={
   prototype:{
      init:function(){
        console.log('Model.prototype.init');
      },
      find:function(){
        console.log('Model.prototype.find');
      }
   },
   inherited:function(){
      //console.log('exec inherited')
   },
   created:function(){
      //console.log('exec created!');
   },
   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){
      for(var key in o){
        this[key]=o[key];
      }
   },
   include:function(o){
      for(var key in o){
         this.prototype[key]=o[key];
      }
   }
};
//类方法
Model.extend({
   records:{},
   initattr:function(o){
      var obj=this.init();
      for(var key in o){
        obj[key]=o[key];
      }
      return obj;
   },
   find:function(id){
      return this.records[id];
   }
});
Model.include({
   save:function(){
      this.parent.records[this.id]=this;
   }
});
var User=Model.create();
//类实例方法
User.include({
   getname:function(){
      console.log(this.name);
   },
   attributes:function(){
      var result={};
      for(var i in this.parent.attributes){
         var attr=this.parent.attributes[i];
         result[attr]=this[attr];
      }
      return result;
   }
});
User.extend({
  attributes:["name","age","id"],
  //保存本地数据
  savelocal:function(){
     var result=[];
     for(var i in this.records){
        var record=this.records[i].attributes();
        result.push(record);
     }
     localStorage.setItem("Users",JSON.stringify(result));
  },
  //加载本地数据
  loadlocal:function(){
     if (localStorage.getItem("Users")){
         alert(localStorage.getItem("Users"));
         var result=JSON.parse(localStorage.getItem("Users"));
         var html=[];
         for(var i in result){
           html.push("<tr>");
           for(var key in this.attributes){
              var attr=this.attributes[key];
              html.push("<td>");
              html.push(result[i][attr]);
              html.push("</td>");
           }
           html.push("</tr>");
         }
         $("#tabinfo tbody").append(html.join(''));
     }
  }
});
$(function(){
    $(window).bind("beforeunload",function(){
       User.savelocal();//页面关闭的时候保存数据
    });
    $("#butadd").click(function(){
       var name=$("#txtname").val();
       var id=$("#txtid").val();
       var age=$("#txtage").val();
       var user=User.initattr({"name":name,"age":age,"id":id});
       user.save();
       var html=[];
       html.push("<tr>");
       for(var key in User.attributes){
              var attr=User.attributes[key];
              html.push("<td>");
              html.push(user[attr]);
              html.push("</td>");
       }
      html.push("</tr>");
      console.log(user);
      $("#tabinfo tbody").append(html.join(''));
    });
    //初始化的时候从本地数据里加载内容
    User.loadlocal();
});
  </script>
 </head>

 <body>
    <p>
      编号:<input type="textbox" id="txtid" style="margin:5px;padding:5px;width:200px;" />
      姓名:<input type="textbox" id="txtname" style="margin:5px;padding:5px;width:200px;" />
      年龄:<input type="textbox" id="txtage" style="margin:5px;padding:5px;width:200px;" />
      <input type="button" id="butadd" style="margin:5px;padding:5px;width:200px;" value="添加" />
    </p>
    <p>
      <table style="border:1px solid green" id="tabinfo">
         <thead>
            <th>编号</th>
            <th>姓名</th>
            <th>年龄</th>
         </thead>
         <tbody>

         </tbody>
      </table>
    </p>
 </body>
</html>

将上面的文件保存为index.html,然后可以在支持html5的浏览器里运行,比如chrome,ff等。还有一个jquery1.8.3的js库,此处可以换成jquery的CDN,这是官方提供的CDN地址http://code.jquery.com/jquery-1.8.3.min.js

好了,今天就写到这了,下次continue :)


目录
相关文章
|
3月前
|
前端开发 JavaScript
个人征信电子版无痕修改, 个人信用报告pdf修改,js+html+css即可实现【仅供学习用途】
本代码展示了一个信用知识学习系统的前端实现,包含评分计算、因素分析和建议生成功能。所有数据均为模拟生成
|
3月前
|
前端开发
个人征信PDF无痕修改软件,个人征信模板可编辑,个人征信报告p图神器【js+html+css仅供学习用途】
这是一款信用知识学习系统,旨在帮助用户了解征信基本概念、信用评分计算原理及信用行为影响。系统通过模拟数据生成信用报告,涵盖还款记录
|
4月前
|
JavaScript 数据可视化 前端开发
three.js简单实现一个3D三角函数学习理解
1.Three.js简介 Three.js是一个基于JavaScript编写的开源3D图形库,利用WebGL技术在网页上渲染3D图形。它提供了许多高级功能,如几何体、纹理、光照、阴影等,以便开发者能够快速地创建复杂且逼真的3D场景。同时,Three.js还具有很好的跨平台和跨浏览器兼容性,让用户无需安装任何插件就可以在现代浏览器上观看3D内容。
169 0
|
11月前
|
JavaScript 前端开发 开发者
VUE 开发——Node.js学习(一)
VUE 开发——Node.js学习(一)
211 2
|
11月前
|
JavaScript
js学习--制作猜数字
js学习--制作猜数字
95 4
js学习--制作猜数字
|
10月前
|
Web App开发 JavaScript 前端开发
如何学习JavaScript?
如何学习JavaScript?
190 5
|
11月前
|
JavaScript
webpack学习五:webpack的配置文件webpack.config.js分离,分离成开发环境配置文件和生产环境配置文件
这篇文章介绍了如何将webpack的配置文件分离成开发环境和生产环境的配置文件,以提高打包效率。
176 1
webpack学习五:webpack的配置文件webpack.config.js分离,分离成开发环境配置文件和生产环境配置文件
|
10月前
|
JavaScript 前端开发 索引
JavaScript学习第二章--字符串
本文介绍了JavaScript中的字符串处理,包括普通字符串和模板字符串的使用方法及常见字符串操作方法如`charAt`、`concat`、`endsWith`等,适合前端学习者参考。作者是一位热爱前端技术的大一学生,专注于分享实用的编程技巧。
85 2
|
10月前
|
存储 JavaScript 前端开发
JavaScript学习第一章
本文档介绍了JavaScript的基础知识,包括其在网页中的作用、如何通过JavaScript动态设置HTML元素的CSS属性,以及JavaScript中的变量类型(`var`、`let`、`const`)和数据类型(基本数据类型与引用数据类型)。通过实例代码详细解释了JavaScript的核心概念,适合初学者入门学习。
126 1