Skip to content

Instantly share code, notes, and snippets.

@timmyomahony
Created June 20, 2016 16:35
Show Gist options
  • Select an option

  • Save timmyomahony/e26ab2cf0ee1df839cfbea5877c4fa62 to your computer and use it in GitHub Desktop.

Select an option

Save timmyomahony/e26ab2cf0ee1df839cfbea5877c4fa62 to your computer and use it in GitHub Desktop.

Revisions

  1. timmyomahony created this gist Jun 20, 2016.
    51 changes: 51 additions & 0 deletions components.list-pagination.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    tagName: "section",
    page: 1,
    paginateBy: 10,
    paginatedItems: Ember.computed('amenities', 'page', function(){
    var i = (parseInt(this.get('page')) - 1) * parseInt(this.get('paginateBy'));
    var j = i + parseInt(this.get('paginateBy'));
    return this.get('items').slice(i, j);
    }),
    numberOfPages: Ember.computed('page', function(){
    var n = this.get('items.length');
    var c = parseInt(this.get('paginateBy'));
    var r = Math.floor(n/c);
    if(n % c > 0) {
    r += 1;
    }
    return r;
    }),
    pageNumbers: Ember.computed('numberOfPages', function(){
    var n = Array(this.get('numberOfPages'));
    for(var i = 0;i < n.length;i++) {
    n[i] = i + 1;
    }
    return n;
    }),
    showNext: Ember.computed('page', function(){
    return (this.get('page') < this.get('numberOfPages'));
    }),
    showPrevious: Ember.computed('page', function(){
    return (this.get('page') > 1);
    }),
    nextText: 'Next page',
    previousText: 'Previous page',
    actions: {
    nextClicked() {
    if(this.get('page') + 1 <= this.get('numberOfPages')) {
    this.set('page', this.get('page') + 1);
    }
    },
    previousClicked() {
    if(this.get('page') > 0) {
    this.set('page', this.get('page') - 1);
    }
    },
    pageClicked(pageNumber){
    this.set('page', pageNumber);
    }
    }
    });
    5 changes: 5 additions & 0 deletions components.team-list-item.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    tagName: "li"
    });
    5 changes: 5 additions & 0 deletions components.team-list.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import Ember from 'ember';

    export default Ember.Component.extend({
    tagName: "ul"
    });
    5 changes: 5 additions & 0 deletions controllers.application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import Ember from 'ember';

    export default Ember.Controller.extend({
    paginateBy: 6
    });
    105 changes: 105 additions & 0 deletions routes.application.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    import Ember from 'ember';

    let teams = [{
    id: 1,
    name: "Albania"
    },
    {
    id: 2,
    name: "Austria"
    },
    {
    id: 3,
    name: "Belgium"
    },
    {
    id: 4,
    name: "Croatia"
    },
    {
    id: 5,
    name: "Czech Republic"
    },
    {
    id: 6,
    name: "England"
    },
    {
    id: 7,
    name: "France"
    },
    {
    id: 8,
    name: "Germany"
    },
    {
    id: 9,
    name: "Hungary"
    },
    {
    id: 10,
    name: "Iceland"
    },
    {
    id: 11,
    name: "Italy"
    },
    {
    id: 12,
    name: "Northern Ireland"
    },
    {
    id: 13,
    name: "Poland"
    },
    {
    id: 14,
    name: "Portugal"
    },
    {
    id: 15,
    name: "Republic of Ireland"
    },
    {
    id: 16,
    name: "Romania"
    },
    {
    id: 17,
    name: "Russia"
    },
    {
    id: 18,
    name: "Slovakia"
    },
    {
    id: 19,
    name: "Spain"
    },
    {
    id: 20,
    name: "Sweden"
    },
    {
    id: 21,
    name: "Switzerland"
    },
    {
    id: 22,
    name: "Turkey"
    },
    {
    id: 23,
    name: "Ukraine"
    },
    {
    id: 24,
    name: "Wales"
    }
    ]

    export default Ember.Route.extend({
    model() {
    return teams;
    }
    });
    11 changes: 11 additions & 0 deletions templates.application.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <p>This is an example showing how to create your own simple pagination component with Ember.</p>
    <hr />
    <p><strong>Euro 2016 teams:</strong></p>
    {{#list-pagination paginateBy=paginateBy items=model as |teams|}}
    {{#team-list teams=teams as |team|}}
    {{#team-list-item}}
    {{team.name}}
    {{/team-list-item}}
    {{/team-list}}
    {{/list-pagination}}
    <hr />
    16 changes: 16 additions & 0 deletions templates.components.list-pagination.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {{yield paginatedItems}}
    <ul>
    {{#if showPrevious}}
    <li><button {{action "previousClicked"}}>{{previousText}}</button></li>
    {{else}}
    <li style="text-decoration: line-through;">{{previousText}}</li>
    {{/if}}
    {{#each pageNumbers as |pageNumber|}}
    <li><button {{action "pageClicked" pageNumber}}>Page {{pageNumber}}</button></li>
    {{/each}}
    {{#if showNext}}
    <li><button {{action "nextClicked"}}>{{nextText}}</button></li>
    {{else}}
    <li style="text-decoration: line-through;">{{nextText}}</li>
    {{/if}}
    </ul>
    1 change: 1 addition & 0 deletions templates.components.team-list-item.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    {{yield}}
    3 changes: 3 additions & 0 deletions templates.components.team-list.hbs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    {{#each teams as |team|}}
    {{yield team}}
    {{/each}}
    16 changes: 16 additions & 0 deletions twiddle.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {
    "version": "0.9.3",
    "EmberENV": {
    "FEATURES": {}
    },
    "options": {
    "use_pods": false,
    "enable-testing": false
    },
    "dependencies": {
    "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
    "ember": "2.6.0",
    "ember-data": "2.6.1",
    "ember-template-compiler": "2.6.0"
    }
    }