-
-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(cookbook): add sample for externally provided model #527
Open
RedTn
wants to merge
12
commits into
adopted-ember-addons:master
Choose a base branch
from
RedTn:docs/external-model-cookbook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1e887f9
docs(cookbook): add sample for externally provided model
RedTn 5ad40da
fix(cookbook): external model rows set in init
a387f46
chore: merge with latest
f4a1048
fix(tests): use proper import for mixin
2a2d865
Revert "fix(tests): use proper import for mixin"
RedTn 0446be1
Revert "chore: merge with latest"
RedTn a467bd2
chore: merge with latest
RedTn 5eac837
fix(tests): use mixin object import
RedTn 5e7d2d3
Revert "chore: merge with latest"
RedTn ccd935f
Revert "fix(tests): use mixin object import"
RedTn e188778
Merge remote-tracking branch 'refs/remotes/offirgolan/master' into do…
RedTn 5449d07
fix(tests): use mixin object import
RedTn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
tests/dummy/app/components/cookbook/external-model-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// BEGIN-SNIPPET external-model-table | ||
import Component from '@ember/component'; | ||
import { computed } from '@ember/object'; | ||
import Table from 'ember-light-table'; | ||
import { isEmpty } from '@ember/utils'; | ||
import { A } from '@ember/array'; | ||
import diffAttrs from 'ember-diff-attrs'; | ||
|
||
export default Component.extend({ | ||
columns: computed(function() { | ||
return [{ | ||
label: 'Avatar', | ||
valuePath: 'avatar', | ||
width: '60px', | ||
sortable: false, | ||
cellComponent: 'user-avatar' | ||
}, { | ||
label: 'First Name', | ||
valuePath: 'firstName', | ||
width: '150px' | ||
}, { | ||
label: 'Last Name', | ||
valuePath: 'lastName', | ||
width: '150px' | ||
}, { | ||
label: 'Address', | ||
valuePath: 'address' | ||
}, { | ||
label: 'State', | ||
valuePath: 'state' | ||
}, { | ||
label: 'Country', | ||
valuePath: 'country' | ||
}]; | ||
}), | ||
|
||
canLoadMore: true, | ||
enableSync: true, | ||
|
||
meta: null, | ||
table: null, | ||
rows: A([]), | ||
|
||
didReceiveAttrs: diffAttrs('model', function(changedAttrs, ...args) { | ||
this._super(...args); | ||
|
||
if (changedAttrs && changedAttrs.model) { | ||
if (isEmpty(changedAttrs.model[1])) { | ||
this.set('canLoadMore', false); | ||
} else { | ||
this.get('rows').pushObjects(changedAttrs.model[1].toArray()); | ||
this.set('meta', changedAttrs.model[1].get('meta')); | ||
} | ||
} | ||
}), | ||
|
||
init() { | ||
this._super(...arguments); | ||
|
||
let table = new Table(this.get('columns'), this.get('rows'), { enableSync: this.get('enableSync') }); | ||
let sortColumn = table.get('allColumns').findBy('valuePath', this.get('sort')); | ||
|
||
// Setup initial sort column | ||
if (sortColumn) { | ||
sortColumn.set('sorted', true); | ||
} | ||
|
||
this.set('table', table); | ||
}, | ||
|
||
willDestroyElement() { | ||
this._super(...arguments); | ||
|
||
this.get('rows').clear(); | ||
}, | ||
|
||
actions: { | ||
onScrolledToBottom() { | ||
if (this.get('canLoadMore')) { | ||
this.get('fetchMore')(); | ||
} | ||
}, | ||
|
||
onColumnClick(column) { | ||
if (column.sorted) { | ||
this.set('canLoadMore', true); | ||
this.get('onSort')({ | ||
dir: column.ascending ? 'asc' : 'desc', | ||
sort: column.get('valuePath'), | ||
page: 1 | ||
}); | ||
this.get('rows').clear(); | ||
} | ||
} | ||
} | ||
}); | ||
// END-SNIPPET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// BEGIN-SNIPPET route-common | ||
import Ember from 'ember'; | ||
import { task } from 'ember-concurrency'; | ||
|
||
export default Ember.Mixin.create({ | ||
page: 1, | ||
limit: 10, | ||
dir: 'asc', | ||
sort: 'firstName', | ||
|
||
model() { | ||
return this.get('fetchRecords').perform(); | ||
}, | ||
|
||
setupController(controller, model) { | ||
this._super(controller, model); | ||
controller.setProperties({ | ||
fetchRecords: this.get('fetchRecords'), | ||
sort: this.get('sort') | ||
}); | ||
}, | ||
resetController(controller, isExiting) { | ||
if (isExiting) { | ||
this.set('page', 1); | ||
} | ||
}, | ||
|
||
actions: { | ||
fetchMore() { | ||
this.incrementProperty('page'); | ||
this.refresh(); | ||
}, | ||
|
||
onSort({ dir = 'asc', sort = 'firstName', page = 1 }) { | ||
this.setProperties({ | ||
dir, | ||
sort, | ||
page | ||
}); | ||
this.refresh(); | ||
} | ||
}, | ||
|
||
fetchRecords: task(function*() { | ||
return yield this.get('store').query('user', this.getProperties(['page', 'limit', 'sort', 'dir'])); | ||
}).restartable() | ||
}); | ||
// END-SNIPPET |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
import EmberRouter from '@ember/routing/router'; | ||
import config from './config/environment'; | ||
|
||
// eslint-disable-next-line ember-suave/no-direct-property-access | ||
const Router = EmberRouter.extend({ | ||
location: config.locationType, | ||
rootURL: config.rootURL | ||
}); | ||
|
||
Router.map(function() { | ||
this.route('responsive'); | ||
this.route('scrolling'); | ||
|
||
this.route('columns', function() { | ||
this.route('grouped'); | ||
this.route('resizable'); | ||
this.route('draggable'); | ||
}); | ||
|
||
this.route('rows', function() { | ||
this.route('expandable'); | ||
this.route('selectable'); | ||
}); | ||
|
||
this.route('cookbook', function() { | ||
this.route('client-side'); | ||
this.route('custom-row'); | ||
this.route('horizontal-scrolling'); | ||
this.route('occlusion-rendering'); | ||
this.route('pagination'); | ||
this.route('table-actions'); | ||
this.route('custom-sort-icon'); | ||
}); | ||
}); | ||
|
||
export default Router; | ||
import EmberRouter from '@ember/routing/router'; | ||
import config from './config/environment'; | ||
|
||
// eslint-disable-next-line ember-suave/no-direct-property-access | ||
const Router = EmberRouter.extend({ | ||
location: config.locationType, | ||
rootURL: config.rootURL | ||
}); | ||
|
||
Router.map(function() { | ||
this.route('responsive'); | ||
this.route('scrolling'); | ||
|
||
this.route('columns', function() { | ||
this.route('grouped'); | ||
this.route('resizable'); | ||
this.route('draggable'); | ||
}); | ||
|
||
this.route('rows', function() { | ||
this.route('expandable'); | ||
this.route('selectable'); | ||
}); | ||
|
||
this.route('cookbook', function() { | ||
this.route('client-side'); | ||
this.route('custom-row'); | ||
this.route('horizontal-scrolling'); | ||
this.route('occlusion-rendering'); | ||
this.route('pagination'); | ||
this.route('table-actions'); | ||
this.route('custom-sort-icon'); | ||
this.route('external-model-table'); | ||
}); | ||
}); | ||
|
||
export default Router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Route from '@ember/routing/route'; | ||
import RouteCommon from '../../mixins/route-common'; | ||
|
||
export default Route.extend(RouteCommon, { | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
tests/dummy/app/templates/components/cookbook/external-model-table.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{{!-- BEGIN-SNIPPET external-model-table --}} | ||
{{#light-table table height='65vh' as |t|}} | ||
|
||
{{!-- | ||
In order for `fa-sort-asc` and `fa-sort-desc` icons to work, | ||
you need to have ember-font-awesome installed or manually include | ||
the font-awesome assets, e.g. via a CDN. | ||
--}} | ||
|
||
{{t.head | ||
onColumnClick=(action 'onColumnClick') | ||
iconSortable='fa fa-sort' | ||
iconAscending='fa fa-sort-asc' | ||
iconDescending='fa fa-sort-desc' | ||
fixed=true | ||
}} | ||
|
||
{{#t.body | ||
canSelect=false | ||
onScrolledToBottom=(action 'onScrolledToBottom') | ||
as |body| | ||
}} | ||
{{#if isLoading}} | ||
{{#body.loader}} | ||
{{table-loader}} | ||
{{/body.loader}} | ||
{{/if}} | ||
{{/t.body}} | ||
|
||
{{/light-table}} | ||
{{!-- END-SNIPPET --}} |
14 changes: 14 additions & 0 deletions
14
tests/dummy/app/templates/cookbook/external-model-table.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{{!-- BEGIN-SNIPPET external-model-table-route --}} | ||
{{#code-panel | ||
title="Externally Provided Model" | ||
snippets=(array | ||
"external-model-table.js" | ||
"route-common.js" | ||
"external-model-table.hbs" | ||
"external-model-table-route.hbs" | ||
"user-avatar.hbs" | ||
"table-loader.hbs" | ||
)}} | ||
{{cookbook/external-model-table model=model onSort=(route-action "onSort") fetchMore=(route-action "fetchMore") isLoading=fetchRecords.isRunning sort=sort}} | ||
{{/code-panel}} | ||
{{!-- END-SNIPPET --}} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth switching to ember-did-change-attrs at some point but didn't now since it's still a WIP.