index.vue 4.51 KB
<template>
    <div>
        <Sort :config="sortConfig" :val="sort">
        </Sort>
        <List :data="productList"></List>
        <Drawer v-ref:drawer>
            <Filter :config="filterConfig"></Filter>
        </Drawer>
    </div>
</template>
<script>
    const Vue = require('yoho-vue');
    const lazyload = require('yoho-vue-lazyload');
    const infinitScroll = require('yoho-vue-infinite-scroll');
    const bus = require('common/vue-bus');
    const tip = require('common/tip');
    const sort = require('component/product/sort.vue');
    const list = require('component/product/list.vue');
    const drawer = require('component/product/drawer.vue');
    const filter = require('component/product/filter.vue');

    Vue.use(lazyload);
    Vue.use(infinitScroll);

    require('common/vue-filter');

    module.exports = {
        el: '#product-list',
        data: function() {
            return {
                sortConfig: [],
                filterConfig: null,

                // query
                url: '/product/list.json',
                sort: '',
                filter: {},
                page: 0, // 未搜索 page=0; 全部加载完 page = totalPage; 无数据: page !=0 && productList.length=0
                totalPage: null,

                // 产品列表
                productList: [],

                // state
                inSearching: false // 请求中
            };
        },
        components: {
            list,
            sort,
            filter,
            drawer
        },
        methods: {
            search: function() {
                const self = this;
                const nextPage = this.page + 1;

                if (this.inSearching) {
                    return;
                }

                // page = 0, 始终执行搜索
                if (this.page && nextPage > this.totalPage) {
                    return;
                }

                this.inSearching = true;
                console.log(nextPage);
                $.get(this.url, {
                    sort: this.sort,
                    filter: this.filter,
                    page: nextPage
                })
                .done(res => {
                    if (res.code === 200) {
                        self.page = res.data.page;
                        self.totalPage = res.data.pageTotal;
                        self.$set('productList', self.productList.concat(res.data.productList));

                        if (!self.filterConfig) {
                            self.$set('filterConfig', res.data.filter);
                        }
                    }
                })
                .fail(error => {
                    tip('网络出错~');
                })
                .always(() => {
                    self.inSearching = false;
                });
            },

            openFilterSub: function(classify) {
                console.log('TODO: open filter sub', classify);
            },

            /**
             *  清空数据(page=0) 重新搜索
             */
            research: function() {
                this.page = 0;
                this.$set('productList', []);
                this.search();
            }
        },
        watch: {
            /* sort 和 filter 改变 都会触发 重新搜索 */
            sort: function() {
                this.research();
            },
            filter: function() {
                this.research();
            }
        },

        created: function() {
            const self = this;


            bus.$on('list.paging', function() {
                self.search();
            });

            bus.$on('sort.change', function({
                val
            }) {
                console.log(val);
                self.sort = val;
            });

            /**
             * 筛选组件 筛选值变更,触发 filter.change事件
             *  1. 重新搜索
             *  2. 关闭 drawer 组件
             */
            bus.$on('filter.change', function({
                val
            }) {
                console.log(val);
                self.filter = val;
                self.$refs.drawer.on = false;
            });

            /**
             *  筛选组件 打开二级晒寻,通过bridge 打开APP view
             *  1. 打开view
             *  2. 监听 router.back ,重新设置 筛选值
             */
            bus.$on('filter.sub.show', function({
                val
            }) {
                self.openFilterSub(val);
            });

            this.search();
        }
    };

</script>
<style>


</style>