
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
ember-cli-sparse-array
Advanced tools
An implementation of SparseArray for Ember.js configured as an addon so that you may use it in any of your EmberCLI projects.
ember install ember-cli-sparse-array
ember g emer-cli-sparse-array
This will create an addon namespace for your application, 'ember-cli-sparse-array'. Available in this namespace is a lib/sparse-array
, which gives your code an array definition to work with.
import Ember from 'ember';
import SparseArray from 'ember-cli-sparse-array/lib/sparse-array';
export default SparseArray.extend({
// Implement your `load` callback ;)
})
You can return the sparse array to a route's model
hook (or afterModel
or beforeModel
depending on your needs) if you wish your route's controller's model to be the content of a lazy-loadable sparse array.
You can use the a route's ...controllerFor('my_route').set('model', mySparseArray);
to do your bidding... You just neet do implement the load
callback.
When instantiating a SparseArray
, you have to supply a load
method. The load
method is responsible for fetching a subset of the resource, e.g. from your server.
The load
method must accept two arguments:
offset
: The 0-based index/offset of the first record to be fetched.limit
: How many records should be fetchedThe load
method must return a thenable (i.e. a promise or another object with a .then
method on it) that eventually resolves with a hash with two keys:
total
: The total number of records.items
: An array of maximum limit
items that start at offset
.Here is an example:
var SparseArray = require('ember-cli-sparse-array/lib/sparse-array');
var comments = SparseArray.create({
load: function(offset, limit) {
return new Em.RSVP.Promise(function(resolve) {
$.getJSON('/comments?offset='+offset+'&limit='+limit).then(function(payload) {
resolve({
total: payload.meta.total,
items: payload.comments
});
});
});
}
});
The SparseArray
will automatically call your load
method when items at not-yet-loaded indexes are being requested by your application.
The length
attribute of your SparseArray
works just like a normal array. It will start out as 0
. It will be set to the value of total
each time a promise from load
resolves.
load
is always called instantly with offset
being 0
when you instantiate a SparseArray
. This is to find the length
property right away.
The SparseArray
also has a property called isLoaded
, which is false
until the first time a load
completes, where it will be set to true
, and stay true
forever.
Calling .objectAt
at an index that's greater than or equals to the current length
, will always return null
.
Example:
var comments = SparseArray.create({
load: function(offset, limit) {/* ... */}
});
comments.get('length'); //Will always be 0, since the data hasn't been loaded yet
comments.get('isLoaded'); //false
comments.objectAt(0); //null
comments.objectAt(9999999); //null
//Later, after the first load has completed:
comments.get('length'); //The `total` value that you resolved the promise with
comments.get('isLoaded'); //true
comments.objectAt(0); //Whatever you returned at the index 0
After the load
method has completed at least once, and the array has a length
property, requesting an index less than length
, will make the array fetch the items around that index using the load
method, if the index
has not
been loaded yet.
The {{each}}
Handlebars helper is "eager", meaning that it will insert views for all items right away. So when your
load promise resolves with a total
value of 9000, it will create 9000 views right away, and request each of the 9000
items from your array. This may result in a lot of requests.
The real power of sparse arrays is when using them together with container views that only requests (i.e. calls
.objectAt
) items that is supposed to be in the browser's current viewport. A good example is
Ember.ListView (disclaimer: This sparse array implementation has not been tested
with Ember.ListView yet, but you get the idea).
You can set other options than the load
method when instantiating SparseArray
. Example:
SparseArray.create({
batchSize: 42,
load: function() {/* ... */}
});
The following options are supported:
batchSize
(integer): The maximum number of records the sparse array will ask the load
method to load through the limit
argument. The actual value of limit
may be lower, since the sparse array will only load items that have not already
been loaded. Defaults to 100
.load
method are currently not handled.objectAt
logic is difficult).Fork and PR :)
Above in "Known limitations" is a good place to start picking up issues.
FAQs
A sparse array implementation to use in Ember.js projects.
We found that ember-cli-sparse-array demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.