sorting - Emberjs advanced sort hasMany association as a computed property -
i have asked variant of question here. need create computed property operated on hasmany association. need sorting similar javascript sort function; can
files = ["file 5", "file 1", "file 3", "file 2"]; files.sort(function(a,b){ return parseint(b.split(' ').pop()) - parseint(a.split(' ').pop()) });
result:
["file 5", "file 3", "file 2", "file 1"]
here jsbin: http://emberjs.jsbin.com/simayexose/edit?html,js,output
any appreciated.
note: jsbin presently not working correctly (for reasons other question). have posted question here. did not want hold answer question.
update 1
thanks @engma. implemented instructions. matter of fact, copied , pasted posted. new jsbin. http://emberjs.jsbin.com/roqixemuyi/1/edit?html,js,output
i still not sorted, though. , if did, still not have sorted way it.
i need following: (below errors when try implement in code, not jsbin, since can not jsbin work)
sortedfiles: function(){ return this.get('files').sort(function(a,b){ return parseint(b.split(' ').pop()) - parseint(a.split(' ').pop()); }); }.property('files.@each.name')
when following error:
uncaught typeerror: this.get(...).sort not function
so since this.get('files')
returns promise, figured try this;
sortedfiles: function(){ return this.get('files').then(function(files){ return files.sort(function(a,b){ return parseint(b.split(' ').pop()) - parseint(a.split(' ').pop()); }); }); }.property('files.@each.name')
but following error:
uncaught error: assertion failed: value #each loops on must array. passed {_id: 243, _label: undefined, _state: undefined, _result: undefined, _subscribers: }
btw, using emberjs v1.11.0
and, sortby using ember-cli/node_modules/bower-config/node_modules/mout/array/sortby.js
here code it
var sort = require('./sort'); var makeiterator = require('../function/makeiterator_'); /* * sort array result of callback */ function sortby(arr, callback, context){ callback = makeiterator(callback, context); return sort(arr, function(a, b) { = callback(a); b = callback(b); return (a < b) ? -1 : ((a > b) ? 1 : 0); }); } module.exports = sortby;
update 2
so answer question how emberjs advanced sort hasmany association computed property; had change
this.get('files').sort(function(a,b){ ... }); return this.get('files').toarray().sort(function(a,b){ ... });
this allowed me use javascript sort , return desired sorted objects.
ok first of jsbin had many issues lets go throw them 1 one
1- did not include ember-data build, included 1, needed fixtures , models
<script src="http://builds.emberjs.com/tags/v1.0.0-beta.15/ember-data.js"></script>
2- scripts
var app = window.app = ember.application.create({ }); //first how register adapter app.applicationadapter = ds.fixtureadapter.extend({}); app.indexroute = ember.route.extend({ model: function() { //second find pass in id using 1 //if want folders use findall() return this.store.find('folder',1); } }); app.indexcontroller = ember.controller.extend({ }); app.router.map(function() { }); app.folder = ds.model.extend({ name: ds.attr('string'), files: ds.hasmany('file',{async:true}), sortedfiles: function(){ //sorty has no second parameter, if need more sorting power, self return this.get('files').sortby('name'); }.property('files.@each.name') }); app.file = ds.model.extend({ name: ds.attr('string'), folder: ds.belongsto('folder',{async:true}) }); app.file.fixtures = [ { id: 1, name: 'file 5', folder:1 }, { id: 2, name: 'file 1', folder:1 }, { id: 3, name: 'file 3', folder:1 }, { id: 4, name: 'file 2', folder:2 }, { id: 5, name: 'file 6', folder:2 }, { id: 6, name: 'file 4', folder:2 } ]; app.folder.fixtures = [ { id: 1, name: 'folder 1', files:[1,2,3] }, { id: 2, name: 'folder 2', files:[4,5,6] } ];
your template:
<div> folders: <br> <ul> <li> name: {{model.name}} <br> files: {{!-- here access sorted files property in model--}} {{#each file in model.sortedfiles}} {{file.name}} <br/> {{/each}} </li> </ul> </div>
Comments
Post a Comment