Authored by weiqingting

代码审查

var $ = require('jquery');
function infiniteLoad(options){
var defaults={
index:0,
isload:true,//是否正在加载
isrun:true,//判断是否执行
offset:{
height:new Function(),
width:new Function()
function infiniteLoad(options) {
var defaults = {
index: 0,
isload: true,//是否正在加载
isrun: true,//判断是否执行
offset: {
height: null,
width: null
}
};
this.registerEvent = {
before: [],
change: [],
after: []
};
this.options = $.extend(true, {}, defaults, options);
return this;
};
infiniteLoad.prototype.on = function (name, callback) {
}
infiniteLoad.prototype.on = function(name, callback) {
var g = this;
var _e = g.registerEvent[name];
if (_e) {
_e.push(callback);
}
return _e;
};
infiniteLoad.prototype.off = function (name, callback) {
infiniteLoad.prototype.off = function(name, callback) {
var g = this;
var _e = g.registerEvent[name];
var e = [];
$.each(_e, function (name, _callback) {
$.each(_e, function(name, _callback) {
if (_callback === callback) {
e.push(name);
}
});
$.each(e.reverse(), function (name, _callback) {
$.each(e.reverse(), function(name, _callback) {
_e.splice(_callback, 1);
});
};
infiniteLoad.prototype.exect=function(key,params){
var g=this,p=this.options;
if (g.registerEvent[key]&&g.registerEvent[key].length > 0) {
for (_e in g.registerEvent[key]) {
g.registerEvent[key][_e](params);
infiniteLoad.prototype.exect = function(key, params) {
var g = this;
var e;
if (g.registerEvent[key] && g.registerEvent[key].length > 0) {
for (e in g.registerEvent[key]) {
if (g.registerEvent[key].hasOwnProperty(e)) {
g.registerEvent[key][e](params);
}
}
}
};
infiniteLoad.prototype.init = function() {
var g=this,p=this.options;
function __loadMore(){
if(p.isload&&g.__directionCalculation()){
p.isload=false;
var g = this;
var p = this.options;
function __loadMore() {
if (p.isload && g.__directionCalculation()) {
p.isload = false;
p.index++;
g.exect("after",p);
g.exect('after', p);
}
g.exect("change",p);
g.exect('change', p);
}
g.exect("before",p);
g.exect('before', p);
$(window).scroll(__loadMore);
};
infiniteLoad.prototype.emit=function(){
var g=this,p=this.options;
p.isload=true;
infiniteLoad.prototype.emit = function() {
var p = this.options;
p.isload = true;
};
infiniteLoad.prototype.__directionCalculation=function(){
var g=this,p=this.options;
if(p.offset.height()>0&&$(window).scrollTop() + $(window).height() >= p.offset.height()){
infiniteLoad.prototype.__directionCalculation = function() {
var p = this.options;
if (p.offset.height() > 0 && $(window).scrollTop() + $(window).height() >= p.offset.height()) {
return true;
}
if(p.offset.width()>0&&$(window).scrollLeft() + $(window).width() >= p.offset.width()){
if (p.offset.width() > 0 && $(window).scrollLeft() + $(window).width() >= p.offset.width()) {
return true;
}
return false;
}
module.exports =infiniteLoad;
};
module.exports = infiniteLoad;
\ No newline at end of file
... ...
var $ = require('jquery');
var Lazyload = require('yoho.lazyload');
var Handlebars=require('yoho.handlebars');
var infiniteLoad=require('../common/infiniteLoad');
//var Lazyload = require('yoho.lazyload');
var Handlebars = require('yoho.handlebars');
var InfiniteLoad = require('../common/infiniteLoad');
(function($) {
var $container=$("div.commodity ul");
var $load=$(".loading");
var load=new infiniteLoad({
offset:{
height:function(){return parseFloat($container.offset().top)+parseFloat($container.height())}
var $container = $('div.commodity ul');
var $load = $('.loading');
var load = new InfiniteLoad({
offset: {
height: function() {
return parseFloat($container.offset().top) + parseFloat($container.height());
}
}
});
load.on("after",function(p){
var options={
load.on('after', function(p) {
var options = {
type: 'POST',
url: '/boys/commodity',
data: {
pageIndex: p.index,
pageCount:10,
flag:'boy'
pageCount: 10,
flag: 'boy'
},
success: function(data) {
var code = data.code;
var myTemplate;
if (code === 200) {
var myTemplate = Handlebars.compile(load.tpl);
myTemplate = Handlebars.compile(load.tpl);
$container.append(myTemplate(data.commodity));
//懒加载插件貌似有点问题,图片先直接展示
//Lazyload($container.find('img.lazy'));
load.emit();
}else{
} else {
$load.html('END');
}
},
error: function() {
$load.html('网络断开连接了~');
}
};
$.ajax(options);
//请求数据
});
load.on('before', function() {
});
load.on("before",function(){
//请求模板
load.tpl='{{#each this}}'
+'<li>'
+' {{# state}}'
+' <i class="commodity-tag{{.}}"></i>'
+' {{/ state}}'
+' <a href="{{url}}"><div class="commodity-img"><img class="lazy" data-original="{{img}}" src="{{img}}"/></div>'
+' <p class="commodity-name">{{name}}</p>'
+' <p class="commodity-price"><span>¥{{price}}</span></p>'
+' </a>'
+'</li>'
+'{{/each}}';
load.tpl = '{{#each this}}';
load.tpl += '<li>';
load.tpl += ' {{# state}}';
load.tpl += ' <i class="commodity-tag{{.}}"></i>';
load.tpl += ' {{/ state}}';
load.tpl += ' <a href="{{url}}"><div class="commodity-img">';
load.tpl += ' <img class="lazy" data-original="{{img}}" src="{{img}}"/></div>';
load.tpl += ' <p class="commodity-name">{{name}}</p>';
load.tpl += ' <p class="commodity-price"><span>¥{{price}}</span></p>';
load.tpl += ' </a>';
load.tpl += '</li>';
load.tpl += '{{/each}}';
load.emit();
})
});
load.init();
}($));
... ...