Showing
4 changed files
with
97 additions
and
10 deletions
| @@ -2,6 +2,8 @@ | @@ -2,6 +2,8 @@ | ||
| 2 | import util from '../../utils/util'; | 2 | import util from '../../utils/util'; |
| 3 | import App from '../../utils/api'; | 3 | import App from '../../utils/api'; |
| 4 | const {regeneratorRuntime} = global | 4 | const {regeneratorRuntime} = global |
| 5 | +import PubSub from '../../utils/PubSub' | ||
| 6 | +console.log(PubSub) | ||
| 5 | Page({ | 7 | Page({ |
| 6 | 8 | ||
| 7 | /** | 9 | /** |
| @@ -11,14 +13,33 @@ Page({ | @@ -11,14 +13,33 @@ Page({ | ||
| 11 | sellers:[], | 13 | sellers:[], |
| 12 | hasMore:true, | 14 | hasMore:true, |
| 13 | }, | 15 | }, |
| 14 | - | 16 | + sellers:[], |
| 15 | /** | 17 | /** |
| 16 | * 生命周期函数--监听页面加载 | 18 | * 生命周期函数--监听页面加载 |
| 17 | */ | 19 | */ |
| 18 | onLoad: function (options) { | 20 | onLoad: function (options) { |
| 19 | - | 21 | + PubSub.on( |
| 22 | + 'follow', | ||
| 23 | + data=>{ | ||
| 24 | + console.log('follow.msg',data) | ||
| 25 | + this.formatSellers(data); | ||
| 26 | + } | ||
| 27 | + ) | ||
| 28 | + }, | ||
| 29 | + formatSellers(data){ | ||
| 30 | + let sellers=[]; | ||
| 31 | + this.sellers.forEach(seller=>{ | ||
| 32 | + if(seller.id==data.sid){ | ||
| 33 | + seller.hasFollow=data.hasFollow | ||
| 34 | + } | ||
| 35 | + if(seller.hasFollow){ | ||
| 36 | + sellers.push(seller) | ||
| 37 | + } | ||
| 38 | + }) | ||
| 39 | + this.setData({ | ||
| 40 | + sellers:sellers | ||
| 41 | + }) | ||
| 20 | }, | 42 | }, |
| 21 | - | ||
| 22 | /** | 43 | /** |
| 23 | * 生命周期函数--监听页面初次渲染完成 | 44 | * 生命周期函数--监听页面初次渲染完成 |
| 24 | */ | 45 | */ |
| @@ -30,7 +51,12 @@ Page({ | @@ -30,7 +51,12 @@ Page({ | ||
| 30 | * 生命周期函数--监听页面显示 | 51 | * 生命周期函数--监听页面显示 |
| 31 | */ | 52 | */ |
| 32 | onShow: function () { | 53 | onShow: function () { |
| 33 | - if(this.hasMore){ | 54 | + if(this.hasMore || this.data.sellers.length==0){ |
| 55 | + this.sellers=[] | ||
| 56 | + this.offset=1 | ||
| 57 | + this.setData({ | ||
| 58 | + sellers:[] | ||
| 59 | + }) | ||
| 34 | this.getPos() | 60 | this.getPos() |
| 35 | } | 61 | } |
| 36 | }, | 62 | }, |
| @@ -46,7 +72,7 @@ Page({ | @@ -46,7 +72,7 @@ Page({ | ||
| 46 | * 生命周期函数--监听页面卸载 | 72 | * 生命周期函数--监听页面卸载 |
| 47 | */ | 73 | */ |
| 48 | onUnload: function () { | 74 | onUnload: function () { |
| 49 | - | 75 | + PubSub.off('follow') |
| 50 | }, | 76 | }, |
| 51 | 77 | ||
| 52 | /** | 78 | /** |
| @@ -94,13 +120,14 @@ Page({ | @@ -94,13 +120,14 @@ Page({ | ||
| 94 | }else{ | 120 | }else{ |
| 95 | seller.seller_tel=[] | 121 | seller.seller_tel=[] |
| 96 | } | 122 | } |
| 97 | - | 123 | + seller.hasFollow=true |
| 98 | }) | 124 | }) |
| 99 | - | 125 | + sellers=[...this.data.sellers,...sellers] |
| 100 | this.setData({ | 126 | this.setData({ |
| 101 | - sellers:[...this.data.sellers,...sellers], | 127 | + sellers:sellers, |
| 102 | hasMore:this.hasMore, | 128 | hasMore:this.hasMore, |
| 103 | }) | 129 | }) |
| 130 | + this.sellers=sellers | ||
| 104 | }) | 131 | }) |
| 105 | }, | 132 | }, |
| 106 | getPos(){ | 133 | getPos(){ |
| @@ -6,7 +6,8 @@ | @@ -6,7 +6,8 @@ | ||
| 6 | <view>您还没有收藏任何餐厅哦</view> | 6 | <view>您还没有收藏任何餐厅哦</view> |
| 7 | </view> | 7 | </view> |
| 8 | <view class="follow" wx:else=""> | 8 | <view class="follow" wx:else=""> |
| 9 | - <view class="seller" wx:for="{{sellers}}" wx:key="sellers.key" bindtap="gotoDetail" data-sid="{{item.id}}"> | 9 | + <view class="seller" wx:for="{{sellers}}" wx:key="sellers.key" |
| 10 | + bindtap="gotoDetail" data-sid="{{item.id}}" wx:if="{{item.hasFollow}}"> | ||
| 10 | <sb-tr seller="{{item}}" hasStat="{{true}}"></sb-tr> | 11 | <sb-tr seller="{{item}}" hasStat="{{true}}"></sb-tr> |
| 11 | </view> | 12 | </view> |
| 12 | <view class="seller flex-vc" wx:if="{{!hasMore}}">没有更多了</view> | 13 | <view class="seller flex-vc" wx:if="{{!hasMore}}">没有更多了</view> |
| @@ -2,6 +2,7 @@ | @@ -2,6 +2,7 @@ | ||
| 2 | import util from '../../utils/util'; | 2 | import util from '../../utils/util'; |
| 3 | import App from '../../utils/api'; | 3 | import App from '../../utils/api'; |
| 4 | const {regeneratorRuntime} = global | 4 | const {regeneratorRuntime} = global |
| 5 | +import PubSub from '../../utils/PubSub' | ||
| 5 | Page({ | 6 | Page({ |
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| @@ -135,10 +136,18 @@ Page({ | @@ -135,10 +136,18 @@ Page({ | ||
| 135 | if(this.data.IsCollectionSeller){//已收藏 | 136 | if(this.data.IsCollectionSeller){//已收藏 |
| 136 | param.cancel=1 | 137 | param.cancel=1 |
| 137 | } | 138 | } |
| 139 | + let hasFollow=!this.data.IsCollectionSeller | ||
| 138 | this.setData({ | 140 | this.setData({ |
| 139 | - IsCollectionSeller:!this.data.IsCollectionSeller | 141 | + IsCollectionSeller:hasFollow |
| 140 | }) | 142 | }) |
| 141 | App.Customer.SetCollectionRel(param); | 143 | App.Customer.SetCollectionRel(param); |
| 144 | + PubSub.emit( | ||
| 145 | + 'follow', | ||
| 146 | + { | ||
| 147 | + sid:this.options.id, | ||
| 148 | + hasFollow:hasFollow, | ||
| 149 | + } | ||
| 150 | + ) | ||
| 142 | }, | 151 | }, |
| 143 | getPos(){ | 152 | getPos(){ |
| 144 | this._getPos().then(res => { | 153 | this._getPos().then(res => { |
utils/PubSub.js
0 → 100644
| 1 | +/** | ||
| 2 | + * Created by goto9 on 2019/3/8. | ||
| 3 | + */ | ||
| 4 | +class PubSub { | ||
| 5 | + constructor() { | ||
| 6 | + this.PubSubCache = { | ||
| 7 | + $uid: 0 | ||
| 8 | + }; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + on(type, handler) { | ||
| 12 | + let cache = this.PubSubCache[type] || (this.PubSubCache[type] = {}); | ||
| 13 | + | ||
| 14 | + handler.$uid = handler.$uid || this.PubSubCache.$uid++; | ||
| 15 | + cache[handler.$uid] = handler; | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + emit(type, ...param) { | ||
| 19 | + let cache = this.PubSubCache[type], | ||
| 20 | + key, | ||
| 21 | + tmp; | ||
| 22 | + | ||
| 23 | + if(!cache) return; | ||
| 24 | + | ||
| 25 | + for(key in cache) { | ||
| 26 | + tmp = cache[key]; | ||
| 27 | + cache[key].call(this, ...param); | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + off(type, handler) { | ||
| 32 | + let counter = 0, | ||
| 33 | + $type, | ||
| 34 | + cache = this.PubSubCache[type]; | ||
| 35 | + | ||
| 36 | + if(handler == null) { | ||
| 37 | + if(!cache) return true; | ||
| 38 | + return !!this.PubSubCache[type] && (delete this.PubSubCache[type]); | ||
| 39 | + } else { | ||
| 40 | + !!this.PubSubCache[type] && (delete this.PubSubCache[type][handler.$uid]); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + for($type in cache) { | ||
| 44 | + counter++; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + return !counter && (delete this.PubSubCache[type]); | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | +export default new PubSub() |
-
Please register or login to post a comment