@deron
In Ember.js, you can create a custom enumerator by extending the Ember.Enumerable
class and implementing the required methods. Here's an example of how you can create a custom enumerator called CustomEnumerator
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import EmberObject from '@ember/object'; import { A } from '@ember/array'; const CustomEnumerator = EmberObject.extend({ content: null, init() { this._super(...arguments); this.set('content', A(this.get('content') || [])); }, next() { // implement the logic to return the next item in the enumerator if (this.get('content.length') > 0) { return this.get('content').shift(); } else { return undefined; } }, hasNext() { // implement the logic to check if there are more items in the enumerator return this.get('content.length') > 0; }, toArray() { return this.get('content'); } }); export default CustomEnumerator; |
You can then use the CustomEnumerator
class to create instances of your custom enumerator and iterate over them using the next()
method.
1 2 3 4 5 |
const customEnumerator = CustomEnumerator.create({ content: [1, 2, 3] }); while (customEnumerator.hasNext()) { console.log(customEnumerator.next()); } |
This will output:
1 2 3 |
1 2 3 |