.extend()

Extends the base Collection class by adding/overwriting methods.

extend(extensions)

Parameter

Description

extensions

Object

An object with the methods

Returns

Description

A modified version of the base Collection class with the new methods

You can define methods using normal and/or arrow functions, but in order to access the base class methods with the "this" keyword, you must use a normal function.

const ExtendedPosts = Posts.extend({
  // Add new methods
  chunk(count) {
    const data = this.getAll();

    const newArr = [];

    for (var i = 0; i < data.length; i+=count)
      newArr[i/count] = data.slice(i, i+count);
      
    return newArr;
  },

  // Overwrite existing methods
  create: () => {
    // Whatever you wanna do here.
  }
});

ExtendedPosts.createBulk([
  { content: '1' },
  { content: '2' },
  { content: '3' },
  { content: '4' }
]);

ExtendedPosts.chunk(2); // [ [{content:'1'},{content:'2'}], [{content:'3'},{content:'4'}] ] 

Last updated