Snippets

Rigo B Castro 46odq: Untitled snippet

Created by Rigo B Castro
import {store} from 'store';

function Controller($scope, Store) {
    "ngInject";

    $scope.search = (query = null) => {

        $scope.loadingProducts = true;

        store.getMapper('product').findAll({
            search: query,
            limit: 30
        }).then((response) => {
            $scope.products = response.data;
            $scope.loadingProducts = false;
        }, () => {
            $scope.loadingProducts = false;
        });
    };

    $scope.search();
}
// DataStore is mostly recommended for use in the browser
import {
    DataStore,
    Schema,
    utils,
    Mapper
} from 'js-data'

import HttpAdapter from 'js-data-http';
import _ from 'lodash';

const schemas = require('./schemas')(Schema);
import * as relations from './relations';


const convertToDate = function (record) {
    if (typeof record.created_at === 'string') {
        record.created_at = new Date(record.created_at)
    }
    if (typeof record.updated_at === 'string') {
        record.updated_at = new Date(record.updated_at)
    }
};

export const adapter = new HttpAdapter({
    // Our API sits behind the /api path
    basePath: '/api',
    
});


export const store = new DataStore({
    mapperDefaults: {
        // Override the original to make sure the date properties are actually Date
        // objects
        createRecord (props, opts) {
            const result = this.constructor.prototype.createRecord.call(this, props, opts);
            if (Array.isArray(result)) {
                result.forEach(convertToDate)
            } else if (this.is(result)) {
                convertToDate(result)
            }
            return result
        }
    }
});

store.registerAdapter('http', adapter, {default: true});

// The User Resource
store.defineMapper('team', {
    endpoint: 'teams'
});


store.defineMapper('customer', {
    endpoint: 'customers',
    relations: relations.customer
});


store.defineMapper('city', {
    endpoint: 'cities'
});

store.defineMapper('product', {
    endpoint: 'products'
});

store.defineMapper('productCategory', {
    endpoint: 'products-categories'
});

store.defineMapper('module', {
    endpoint: 'modules'
});

store.defineMapper('assign', {
    endpoint: 'assigns'
});

store.defineMapper('addresses', {
    endpoint: 'customers-addresses'
});

store.defineMapper('serviceType', {
    endpoint: 'services-types'
});

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.