Registry / devops / ampersand-rest-collection

ampersand-rest-collection

JSON →
library6.0.0jsnpmunverified

A collection library extending ampersand-collection with REST API and Lodash utility mixins. Version 6.0.0 is current stable. It provides a Backbone.Collection-like interface for RESTful operations (fetch, create, update, delete) and includes Lodash methods for collection manipulation. Part of the Ampersand.js toolkit for building client-side apps. Differentiates from Backbone by being more modular and emphasizing simplicity.

npm install ampersand-rest-collection
INSTALL
IMPORT
SIG · AMPERSAND-REST-COL
A
ampersand-rest-collection
devopsjavascriptv6.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import Collection from 'ampersand-rest-collection'
const Collection = require('ampersand-rest-collection')
This package supports CommonJS require out of the box, but ESM imports are not officially supported. Use require for guaranteed compatibility.
Collection.extend
const MyCollection = Collection.extend({ model: Model, url: '/api' })
Collection.extend({ model: Model, url: '/api' }) without assigning to a variable
extend() returns a new class; you must assign the result to create a reusable collection class.
fetch
collection.fetch()
Collection.fetch()
fetch is an instance method, not a static method. Call on a collection instance.

Demonstrates creating a custom collection with a model and URL, then fetching data from the server.

const Collection = require('ampersand-rest-collection'); const Model = require('ampersand-state'); const MyModel = Model.extend({ props: { id: 'number', name: 'string' } }); const MyCollection = Collection.extend({ model: MyModel, url: 'https://api.example.com/items' }); const collection = new MyCollection(); collection.fetch({ success: function() { console.log('Fetched models:', collection.models); }, error: function(err) { console.error('Fetch failed:', err); } });
Debug
Known issues
breakingVersion 6.0.0 dropped support for Internet Explorer 8 and below. Requires a browser with ES5 support.
fix
If you need IE8 support, stick with version 5.x.
affects: >=6.0.0
gotchaajaxConfig must be a function if you need dynamic configuration; passing an object directly will not work.
fix
Define ajaxConfig as a function that returns an object, e.g., ajaxConfig: function() { return { headers: { ... } }; }
affects: >=5.0.0
breakingIn version 6.0.0, the default success/error callbacks for fetch no longer parse the response body automatically if a dataType is not specified. Ensure you set dataType: 'json' in options or rely on server response headers.
fix
Pass { dataType: 'json' } in fetch options, or ensure your server returns correct Content-Type header.
affects: >=6.0.0
gotchaWhen using with React or other frameworks that wrap lifecycle, ensure fetch is called after the collection is instantiated; calling fetch in the constructor may cause race conditions.
fix
Call fetch in componentDidMount or equivalent lifecycle method, not in the constructor.
affects: >=5.0.0
Errors
Common errors & fixes
Uncaught TypeError: Collection.extend is not a function
Incorrect import: trying to use a non-existent named export or a default export as a constructor.
fix
Use: const Collection = require('ampersand-rest-collection'); then Collection.extend({...})
XMLHttpRequest cannot load https://api.example.com/data. No 'Access-Control-Allow-Origin' header is present on the requested resource.
Cross-origin request without proper CORS headers from the server.
fix
Configure your server to include Access-Control-Allow-Origin header, or set up a proxy. For IE9, use ajaxConfig: { useXDR: true }.
TypeError: collection.fetch is not a function
Using an ampersand-collection instance without the REST mixin.
fix
Ensure you are using ampersand-rest-collection, not plain ampersand-collection. Check your require/import path.
Upgrade
Version history
6.0.0latest on npm
Audit
Dependencies
ampersand-collectionrequiredCore collection class that this package extends.
ampersand-collection-rest-mixinrequiredProvides RESTful methods like fetch.
ampersand-collection-lodash-mixinrequiredAdds Lodash utility methods to the collection.
ampersand-syncrequiredHandles AJAX requests for REST operations.
Agent activity
20 hits · last 30 days
node
16
Amazon
1
OpenAI (training)
1
Resources