controller.js 1.45 KB
'use strict';

import {
    Controller
} from 'yoho-mvc';

import {
    GetMore
} from './view';

import {
    moreGoods as getMore
} from './model';

const lazyLoad = require('yoho-jquery-lazyload');
const goodContent = require('3party/material/goods.hbs');

class MaterialController extends Controller {
    constructor() {
        super();
        lazyLoad($('img.lazy'));
        this.more = new GetMore();
        this.more.on('more', this.doMore.bind(this));
        this.page = 1;
        this.loading = false;
    }

    doMore() {
        if (!this.end && !this.loading) {
            this.page++;
            this.moreGood(this.page);
        }
    }

    moreGood(page) {
        this.loading = true;
        $('.material-c').append('<p class="show-more good-more">加载更多...</p>');
        getMore('//m.yohobuy.com/3party/material/moreGoods', {
            page: page,
            union_type: window.queryString.union_type
        }).then(data => {
            if (data.goods.length > 0) {
                $('.goods-list').append(goodContent(data));

                // 每次只lazyload倒数10
                lazyLoad($('img.lazy'));
            } else {
                $('.material-c').append('<p class="show-more">没有更多了...</p>');
                this.end = true;
            }
        }).catch(() => {}).finally(() => {
            this.loading = false;
            $('.good-more').remove();
        });
    }
}

module.exports = MaterialController;