0.1.3 • Published 6 years ago

mkd-loadmore v0.1.3

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

Overview

mkd-loadmore is a two-direction mobile pull-to-refresh component for vue.js.

Installation

$ npm install mkd-loadmore

Usage

Import mkd-loadmore to your project:

require('mkd-loadmore/lib/index.css');

// ES6 mudule
import Loadmore from 'mkd-loadmore';

// CommonJS
const Loadmore = require('mkd-loadmore').default;

Register component:

Vue.component('loadmore', Loadmore);

Then use it:

<loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded">
  ...
</loadmore>

Example

Visit this page using your mobile device.

<loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded">
  <ul>
    <li v-for="item in list">{{ item }}</li>
  </ul>
</loadmore>
<loadmore :top-method="loadTop2" :top-status.sync="topStatus">
  <ul>
    <li v-for="item in list2">{{ item }}</li>
  </ul>
  <div slot="top" class="mkd-loadmore-top">
    <span v-show="topStatus !== 'loading'" :class="{ 'rotate': topStatus === 'drop' }">↓</span>
    <span v-show="topStatus === 'loading'">Loading...</span>
  </div>
</loadmore>

For upward direction, pull the component topDistance pixels away from the top and then release it, the function you appointed as top-method will run:

loadTop(id) {
  ...// load more data
  this.$broadcast('onTopLoaded', id);
}

At the end of your top-method, don't forget to broadcast the onTopLoaded event so that mkd-loadmore removes topLoadingText. Note that a parameter called id is passed to loadTop and onTopLoaded. This is because after the top data is loaded, some reposition work is performed inside a mkd-loadmore instance, and id simply tells the component which instance should be repositioned. You don't need to do anything more than passing id to onTopLoaded just as shown above.

For downward direction, things are similar. To invoke bottom-method, just pull the component bottomDistance pixels away from the bottom and then release it.

loadBottom(id) {
  ...// load more data
  this.allLoaded = true;// if all data are loaded
  this.$broadcast('onBottomLoaded', id);
}

Remember to set bottom-all-loaded to true after all data are loaded. And of course broadcast onBottomLoaded with id.

You can customize the top and bottom DOM using an HTML template. For example, to customize the top DOM, you'll need to add a variable that syncs with top-status on loadmore tag, and then write your template with a slot attribute set to top and class set to mkd-loadmore-top. top-status has three possible values that indicates which status the component is at:

  • pull if the component is being pulled yet not ready to drop (top distance is within the distance threshold defined by topDistance)
  • drop if the component is ready to drop
  • loading if topMethod is running

And of course, if a top HTMl template is given, topPullText, topDropText and topLoadingText are all unnecessary.

Don't need to load data from upward direction? Simply omit the topMethod attribute. Same goes to downward.

Upon loaded, loadmore will automatically check if it is tall enough to fill its container. If not, bottom-method will run until its container is filled. Turn off auto-fill if you'd rather handle this manually.

API

OptionDescriptionValueDefault
autoFillif true, loadmore will check and fill its containerBooleantrue
topPullTexttop text when the component is being pulled downString'下拉刷新'
topDropTexttop text when the component is ready to dropString'释放更新'
topLoadingTexttop text while topMethod is runningString'加载中...'
topDistancedistance threshold that triggers topMethodNumber70
topMethodupward load-more functionFunction
bottomPullTextbottom text when the component is being pulled upString'上拉刷新'
bottomDropTextbottom text when the component is ready to dropString'释放更新'
bottomLoadingTextbottom text while bottomMethod is runningString'加载中...'
bottomDistancedistance threshold that triggers bottomMethodNumber70
bottomMethoddownward load-more functionFunction
bottomAllLoadedif true, bottomMethod can no longer be triggeredBooleanfalse

License

MIT