Authored by 郝肖肖

'news-list-MVC'

/**
* 新闻列表
* @author: xiaoxiao<xiaoxiao.hao@yoho.cn>
* @date: 2017/10/18
* @date: 2017/10/20
*/
require('news/index.page.css');
let $ = require('yoho-jquery');
let winH = $(window).height();
let $infos = $('#info-list').children('.info-list');
const NewsIndexController = require('./news-index/controller');
let infoList = require('./info-list');
require('common');
let navState = [{
page: 2,
end: false
}];
function scrollHandler() {
let $c = $infos.not('.hide');
if ($(window).scrollTop() + winH >= $(document).height() - 0.25 * $c.height()) {
infoList.loadMore($c, navState[0]);
}
}
// srcoll to load more
$(document).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});
$(document).ready(function() {
infoList.init();
});
new NewsIndexController();
... ...
'use strict';
import {
Controller
} from 'yoho-mvc';
import {
IndexView
} from './view';
class IndexController extends Controller {
constructor() {
super();
this.init();
}
init() {
new IndexView().init();
}
}
module.exports = IndexController;
... ...
let tip = require('plugin/tip');
let mvc = require('yoho-mvc');
export class IndexModel {
loadMoreAjax(option) {
return mvc.http(option).catch(() => {
tip.show('网络断开了~~');
});
}
}
... ...
import {
View
} from 'yoho-mvc';
import {
IndexModel
} from './model';
let lazyLoad = require('yoho-jquery-lazyload');
let ellipsis = require('yoho-mlellipsis');
export class IndexView extends View {
constructor() {
super();
}
init() {
let that = this;
let $loadMoreInfo = $('#load-more-info');
ellipsis.init();
this.indexModel = new IndexModel();
this.selector = {
searching: false,
$win: $(window),
$infoList: $('#info-list'),
$loading: $loadMoreInfo.children('.loading'),
$noMore: $loadMoreInfo.children('.no-more'),
$infos: $('#info-list').children('.info-list'),
};
this.navState = [{
page: 2,
end: false
}];
// srcoll to load more
$(document).scroll(function() {
window.requestAnimationFrame(function() {
that.scrollHandler.apply(that, []);
});
});
this.setLazyLoadAndMellipsis(this.selector.$infoList.children('.info-list').not('.hide').find('.news-info'));
}
scrollHandler() {
let $c = this.selector.$infos.not('.hide');
if (this.selector.$win.scrollTop() + this.selector.$win.height() >= $(document).height() - 0.25 * $c.height()) {
this.loadMore($c, 0);
}
}
loadMore($container, index) {
let that = this;
if (that.selector.searching || that.navState[index].end) {
return;
}
that.selector.searching = true;
that.selector.$noMore.addClass('hide');
that.selector.$loading.removeClass('hide');
this.indexModel.loadMoreAjax({
type: 'GET',
url: '/news/index/page',
dataType: 'html',
data: that.navState[index]
}).then(rdata => {
that.selector.$loading.addClass('hide');
if (rdata === '') {
that.navState[index].end = true;
that.selector.searching = false;
that.selector.$noMore.removeClass('hide');
return;
}
let $dataHtml = $(rdata);
$container.append($dataHtml);
that.setLazyLoadAndMellipsis($dataHtml.siblings('.news-info'));
that.navState[index].page++;
that.selector.searching = false;
}).catch(() => {
that.selector.searching = false;
});
}
setLazyLoadAndMellipsis($infos) {
lazyLoad($infos.find('img.lazy'));
$infos.each(function() {
let $this = $(this),
$title = $this.find('.info-title'),
$text = $this.find('.info-text');
$title[0].mlellipsis(2);
$text[0].mlellipsis(2);
});
}
}
... ...
... ... @@ -84,6 +84,10 @@ $focus-size: 42px;
.post-content {
padding: 0 30px;
background-color: #fff;
img {
height: auto;
}
}
.text-block {
... ...
... ... @@ -51,7 +51,6 @@
img {
display: block;
min-height: 400px;
}
.play {
... ...