How to use the datx-utils.assignComputed function in datx-utils

To help you get started, we’ve selected a few datx-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github infinum / datx / packages / datx / src / helpers / model / init.ts View on Github external
type: FieldType = FieldType.DATA,
) {
  const fields = getModelMetaKey(obj, 'fields') as Array;

  if (type === FieldType.ID && key in obj) {
    storage.setModelDataKey(obj, key, undefined);
  }

  // Initialize the observable field to the default value
  storage.setModelDataKey(obj, key, defValue);
  updateAction(obj, key, defValue);
  if (fields.indexOf(key) === -1) {
    fields.push(key);
  }

  assignComputed(obj, key,
    () => getField(obj, key),
    (value) => {
      updateField(obj, key, value, type);
    },
  );
}
github infinum / datx / packages / datx / src / helpers / model / init.ts View on Github external
export function initModelRef(obj: PureModel, key: string, options: IReferenceOptions, initialVal: TRefValue) {
  const refs = getModelMetaKey(obj, 'refs');

  // Initialize the observable field to the given value
  refs[key] = options;

  const isArray = options.type === ReferenceType.TO_MANY;
  storage.setModelDataKey(obj, key, isArray ? [] : undefined);
  updateAction(obj, key, isArray ? [] : undefined);

  assignComputed(obj, key,
    () => getRef(obj, key),
    (value) => {
      updateRef(obj, key, value);
    },
  );

  if (!options.property && initialVal !== undefined) {
    obj[key] = initialVal;
  }
}
github infinum / datx / packages / datx / src / CompatModel.ts View on Github external
Object.keys(this.static.refs).forEach((prop) => {
      const refs = getModelMetaKey(this, 'refs');
      if (prop in refs) {
        return;
      }
      const ref = this.static.refs[prop];
      const data = mapItems(this[prop] || this.static.defaults[prop], getModelId);
      assignComputed(this, prop);
      if (typeof ref === 'object') {
        // Back reference
        initModelRef(this, prop, {
          model: ref.model,
          property: ref.property,
          type: ReferenceType.TO_ONE_OR_MANY,
        }, data);
      } else {
        // Normal reference
        initModelRef(this, prop, {
          model: ref,
          type: ReferenceType.TO_ONE_OR_MANY,
        }, data);
      }
    });
github infinum / datx / packages / datx-jsonapi / src / Response.ts View on Github external
Object.keys(this.links).forEach((link: string) => {
        assignComputed(this, link, () => this.__fetchLink(link));
      });
    }
github infinum / datx / packages / datx / src / CompatCollection.ts View on Github external
this.static.types.forEach((model) => {
      const type = getModelType(model);
      assignComputed(this, type.toString(), () => this.findAll(type));
    });
  }

datx-utils

DatX lib utils for mixins

MIT
Latest version published 3 years ago

Package Health Score

60 / 100
Full package analysis