require、backbone等重构手机图片查看器


Posted in Javascript onNovember 17, 2016

本文是对之前的部分补充,也是对最近学习require、backbone的一次实例化的实践,希望对正在学习理解中的同学们有帮助

前文请前往:制作手机使用的网页图片查看器

新手机图片查看器

网页部分

require引入是重点,指明了主函数所在文件路径

<!doctype html>
<html lang="zh-cn">
<head>
<title>webapp图片查看器</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" />
<script src="http://cdn.file1.goodid.com/static/js/require.min.js" data-main="/static/js/pic/main"></script>
</head>
<body>
<section class="index">
 <h1>手机网页图片查看器</h1>
 <figure class="download">
  <a>其它文件</a>
  <a url="http://static.bootcss.com/www/assets/img/opencdn.png">图片a</a>
  <a url="http://static.bootcss.com/www/assets/img/buttons.png">图片b</a>
  <a>其它文件</a>
  <a>其它文件</a>
  <a url="http://static.bootcss.com/www/assets/img/gruntjs.png">图片c</a>
  <a url="http://static.bootcss.com/www/assets/img/lesscss.png">图片d</a>
  <a>其它文件</a>
 </figure>
</section>
</body>
</html>

require.js加载完成后即加载main.js;样式部分就不占篇幅了,后面自己看完整网页 

模版引擎部分

第一个是对话框、第二个是当前位置、第三个是当前展示图片

<script id="dialog_tmpl" type="text/template">
<section></section>
<figure id="swipe"><ul></ul></figure>
<footer>
 <span id="l">左旋</span>
 <span id="r">右旋</span>
 <span id="pos" index="<%=index %>" length="<%=length %>"><%=index %>/<%=length %></span>
</footer>
</script>

<script id="pos_tmpl" type="text/template">
<span id="pos" index="<%=index %>" length="<%=length %>"><%=index %>/<%=length %></span>
</script>

<script id="item_tmpl" type="text/template">
<img src="<%=src %>" width="<%=width %>" height="<%=height %>" url="<%=url %>" />
</script>

3个模版需要写入HTML文件内 

程序开发部分

主函数main.js

require.config({
 paths : {
  jquery  : 'http://cdn.file1.goodid.com/static/js/zepto.min',
  fastclick : 'http://cdn.file1.goodid.com/static/js/fastclick.min',
  underscore : 'http://cdn.file1.goodid.com/static/js/underscore.min',
  backbone : 'http://cdn.file1.goodid.com/static/js/backbone.min',
  swipe  : 'http://cdn.file1.goodid.com/static/js/swipe.min'
 },
 shim : {
  jquery : {
   exports : '$'
  },
  fastclick : {
   deps : ['jquery']
  }
 }
});

require(['underscore', 'backbone', 'fastclick'], function (){
 FastClick.attach(document.body);
 require(['./view/global'], function(Global){
  var global = new Global;
 });
});

paths定义了各模块路径;shim中重新解析了jquery模块,fastclick(一款帮助提高触摸体验的微型插件)指明依赖模块jquery

require首先依次加载underscore、backbone、jquery、fastclick模块,然后再加载全局控制视图global模块并实例化 

全局控制视图global.js

define(['model/pic', 'collection/set', 'view/imager'], function (Pic, Set, Imager){
 var set = new Set;

 // 全局控制视图
 var global = Backbone.View.extend({
  el : 'body',
  data : $('.download [url]'),
  events : {
   'click .download [url]' : 'open'
  },
  open : function (model){
   var url = $(model.target).attr('url');
   var index = this.data.index($(model.target));
   var length = this.data.length;
   var total = new Pic.total({
    index : index + 1,
    length : length
   });
   var dialog = new Imager.dialog({
    model : total
   });
   $(this.el).prepend(dialog.render().el); // 绘制图片查看器

   this.collect();
   this.list();
   this.swipe(index);
   this.loading(url, index);
  },
  collect : function (){
   if(set.length > 0) return false;

   this.data.each(function(){
    var name = $(this).text();
    var url = $(this).attr('url');
    var item = new Pic.item({
     name : name,
     url : url
    });
    set.add(item); // 添加模型
   });
  },
  list : function (){
   var obj = $('#swipe ul');
   set.each(function(model){
    var list = new Imager.list({
     model : model
    });
    obj.append(list.render().el); // 绘制图片列表
   });
  },
  swipe : function (index){
   require(['swipe'], function(){
    var swipe = new Imager.swipe;
    swipe.render(index).el; // 绘制图片滑动特效
   });
  },
  loading : function (url, index){
   var item = new Pic.item({
    url : url
   });
   var loading = new Imager.loading({
    model : item,
    el : $('#swipe li').eq(index).find('img')
   });
   loading.render(); // 绘制图片加载
  }
 });

 return global;
});

依次加载它依赖的数据模型pic模块、数据集合set模块、渲染视图imager模块并实例化了一个集合模块

全局控制视图中我们定义了:绘制图片查看器的open方法、添加模型的collect方法、绘制图片列表的list方法、绘制图片滑动特效的swipe方法、绘制图片加载的loading方法

 渲染视图imager.js

define(['model/pic'], function (Pic){
 var imager = Object;

 // 图片查看器视图
 imager.dialog = Backbone.View.extend({
  initialize : function (){
   _.bindAll(this, 'render');
  },
  tagName : 'section',
  className : 'dialog',
  template : _.template($('#dialog_tmpl').html()),
  events : {
   'click #l, #r' : 'turn'
  },
  render : function (){
   $(this.el).html(this.template(this.model.toJSON()));
   return this;
  },
  turn : function(model){
   var index = parseInt($('#pos').attr('index')) - 1;
   var obj = $('#swipe li').eq(index).find('img');
   var deg = parseInt(obj.attr('deg') ? obj.attr('deg') : 0);
   var type = model.target.id;
   if(type && type == 'l') deg -= 90;
   else if(type && type == 'r') deg += 90;
   if(deg > 360) deg -= 360;
   else if(deg < -360) deg += 360;
   obj.css({'-webkit-transform':'rotate(' + deg + 'deg)'}).attr({'deg':deg});
  }
 });

 // 图片列表视图
 imager.list = Backbone.View.extend({
  initialize : function (){
   _.bindAll(this, 'render');
  },
  tagName : 'li',
  template : _.template($('#item_tmpl').html()),
  events : {
   'click img' : 'close'
  },
  render : function (){
   $(this.el).html(this.template(this.model.toJSON()));
   return this;
  },
  close : function (){
   $('.dialog').remove();
  }
 });

 // 图片滑动定位视图
 imager.fix = Backbone.View.extend({
  initialize : function (){
   _.bindAll(this, 'render');
  },
  el : '#pos',
  template : _.template($('#pos_tmpl').html()),
  render : function (){
   $(this.el).replaceWith(this.template(this.model.toJSON()));
   $('#swipe [deg]').removeAttr('deg').removeAttr('style');
   return this;
  }
 });

 // 图片加载视图
 imager.loading = Backbone.View.extend({
  initialize : function (){
   _.bindAll(this, 'render');
  },
  template : _.template('<img src="<%=url %>" />'),
  render : function (){
   var obj = $(this.el);
   var html = this.template(this.model.toJSON());
   var img = new Image();
   img.src = this.model.attributes.url;
   img.onload = function(){
    obj.replaceWith(html);
   };
   return this;
  }
 });

 // 图片滑动特效视图
 imager.swipe = Backbone.View.extend({
  initialize : function (){
   _.bindAll(this, 'render');
  },
  render : function (index){
   var obj = document.getElementById('swipe');
   window.mySwipe = Swipe(obj, {
    startSlide : index,
    continuous : false,
    disableScroll : true,
    callback  : function(index, element){
     var length = $('#pos').attr('length');
     var total = new Pic.total({
      index : index + 1,
      length : length
     });
     var fix = new imager.fix({
      model : total
     });
     fix.render(); // 绘制图片滑动定位

     var url = $(element).find('img').attr('url');
     if(!url || url.length == 0) return false;

     var item = new Pic.item({
      url : url
     });
     var loading = new imager.loading({
      model : item,
      el : $(element).find('img')
     });
     loading.render(); // 绘制图片加载
    }
   });
   return this;
  }
 });

 return imager;
});

数据模型pic.js

define(function (){
 var pic = Object;

 // 图片数据统计模型
 pic.total = Backbone.Model.extend({
  defaults : {
   index : 1,
   length : 1
  }
 });

 // 图片数据模型
 pic.item = Backbone.Model.extend({
  defaults : {
   name : '图片加载中...',
   src : 'http://cdn.file1.goodid.com/static/images/loading.gif',
   url : '',
   width : 40,
   height : 40
  }
 });

 return pic;
});

数据集合set.js

define(['model/pic'], function (Pic){
 // 图片数据集合
 var set = Backbone.Collection.extend({
  model : Pic.item
 });

 return set;
});

模块定义让程序更加清晰了,模块加载让文件加载执行在我们的掌控之中;MVC模式(C还没用上)让数据逻辑层等分离更加顺手减少了代码混乱。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JS实现点击链接取消跳转效果的方法
Jan 24 Javascript
基于JS实现PHP的sprintf函数实例
Nov 14 Javascript
在其他地方你学不到的jQuery小贴士和技巧(欢迎收藏)
Jan 20 Javascript
jQuery leonaScroll 1.1 自定义滚动条插件(推荐)
Sep 17 Javascript
jQuery select自动选中功能实现方法分析
Nov 28 Javascript
JS操作xml对象转换为Json对象示例
Mar 25 Javascript
运用jQuery写的验证表单(实例讲解)
Jul 06 jQuery
Node.js中流(stream)的使用方法示例
Jul 16 Javascript
vue 1.x 交互实现仿百度下拉列表示例
Oct 21 Javascript
解决Vue 浏览器后退无法触发beforeRouteLeave的问题
Dec 24 Javascript
VUE 使用中踩过的坑
Feb 08 Javascript
JavaScript对象访问器Getter及Setter原理解析
Dec 08 Javascript
基于touch.js手势库+zepto.js插件开发图片查看器(滑动、缩放、双击缩放)
Nov 17 #Javascript
移动端js图片查看器
Nov 17 #Javascript
javascript另类方法实现htmlencode()与htmldecode()函数实例分析
Nov 17 #Javascript
JavaScript实现解析INI文件内容的方法
Nov 17 #Javascript
详解AngularJS中的表单验证(推荐)
Nov 17 #Javascript
JavaScript实现清空(重置)文件类型INPUT元素值的方法
Nov 17 #Javascript
用Vue.js实现监听属性的变化
Nov 17 #Javascript
You might like
深入解析phpCB批量转换的代码示例
2013/06/27 PHP
利用PHP函数计算中英文字符串长度的方法
2014/11/11 PHP
php 如何设置一个严格控制过期时间的session
2017/05/05 PHP
如何让PHP编码更加好看利于阅读
2019/05/12 PHP
php array_map()函数实例用法
2021/03/03 PHP
基于jquery的获取mouse坐标插件的实现代码
2010/04/01 Javascript
JQuery 应用 JQuery.groupTable.js
2010/12/15 Javascript
Jquery Post处理后不进入回调的原因及解决方法
2014/07/15 Javascript
jQuery中serializeArray()与serialize()的区别实例分析
2015/12/09 Javascript
解决jQuery上传插件Uploadify出现Http Error 302错误的方法
2015/12/18 Javascript
angular2使用简单介绍
2016/03/01 Javascript
详解jQuery中的事件
2016/12/14 Javascript
es6+angular1.X+webpack 实现按路由功能打包项目的示例
2017/08/16 Javascript
把vue-router和express项目部署到服务器的方法
2018/02/21 Javascript
Python安装Imaging报错:The _imaging C module is not installed问题解决方法
2014/08/22 Python
浅谈python中截取字符函数strip,lstrip,rstrip
2015/07/17 Python
你应该知道的python列表去重方法
2017/01/17 Python
浅析python3字符串格式化format()函数的简单用法
2018/12/07 Python
django 配置阿里云OSS存储media文件的例子
2019/08/20 Python
python使用pip安装SciPy、SymPy、matplotlib教程
2019/11/20 Python
Python CSS选择器爬取京东网商品信息过程解析
2020/06/01 Python
联想阿根廷官方网站:Lenovo Argentina
2019/10/14 全球购物
Vrbo西班牙:预订您的度假公寓(公寓、乡村房屋…)
2020/04/27 全球购物
运动会领导邀请函
2014/01/10 职场文书
三八节主持词
2014/03/17 职场文书
幼儿园大班评语大全
2014/04/17 职场文书
责任担保书范文
2014/05/21 职场文书
党委班子对照检查材料
2014/08/19 职场文书
英语课外活动总结
2014/08/27 职场文书
中秋晚会活动方案
2014/08/31 职场文书
捐款仪式主持词
2015/07/04 职场文书
企业安全生产规章制度
2015/08/06 职场文书
高中体育课教学反思
2016/02/16 职场文书
Python insert() / append() 用法 Leetcode实战演示
2021/03/31 Python
基于Python实现射击小游戏的制作
2022/04/06 Python
MySQL创建管理子分区
2022/04/13 MySQL