Coder Social home page Coder Social logo

aurelia-date-observer's Introduction

aurelia-date-observer

Fast and efficient Date objects observer plugin for Aurelia Framework.

By default Aurelia can't bind to Date objects and can't efficiently track Date object changes via methods like setFullYear().

This plugin allow to make framework properly bind to Date objects and efficiently track changes.

There are two modes for observe mode:

  1. Property Mode

Will dirty check for valueOf() on Date objects. Fast but not so much as next mode.

  1. Setter Mode

Will bind to pseudo property on Date object which is monitored via setter observer. Blazing fast.

Get Started

  1. Install aurelia-date-observer:
jspm install aurelia-date-observer
  1. Use the plugin in your app's main.js:
export function configure(aurelia) {
  aurelia.use
    .standardConfiguration()
    .developmentLogging();
 
  aurelia.use.plugin('aurelia-date-observer');
  // or
  //aurelia.use.plugin('aurelia-date-observer', options);

  aurelia.start().then(() => aurelia.setRoot());
}

Example

This example will show how Date object binding could be used.

Example below with aurelia-date-observer plugin will work and use 0 resources in background to track model changes.

Without aurelia-date-observer plugin and with @computedFrom('timestamp') annotation Aurelia will not be able to track timestamp changes.

Without both aurelia-date-observer plugin and @computedFrom('timestamp') annotation Aurelia will fall back to dirty checking and will eat resources in background. For example, if it's a huge schedule application with a lot of events on schedule.

The difference could be easily tracked with aurelia-stats plugin.

import {inlineView} from 'aurelia-framework';
import * as moment from 'moment';

@inlineView(`
  <template>
    <div class="event" css.bind="{ top: top + 'px', height: height + 'px' }">
      ${time}
    </div>
  </template>
`)
export class Event {
  timestamp;  // Event start date/time, Date
  duration;   // Event duration in minutes, Number
  
  topOffset = 0;
  hourHeight = 48;

  @computedFrom('timestamp')
  get time() {
    return moment(this.timestamp).format('h:mm A');
  }
  
  @computedFrom('offset', 'topOffset', 'hourHeight')
  get top() {
    return this.topOffset + this.offset * this.hourHeight / 60;
  }

  @computedFrom('duration', 'hourHeight')
  get height() {
    return this.duration * this.hourHeight / 60;
  }
  
  @computedFrom('timestamp')
  get offset() {
    return this.timestamp.getMinutes() + this.timestamp.getHours() * 60;
  }
  
  set offset(value) {
    var hours = Math.floor(value / 60);
    var minutes = Math.round(value % 60);
    // Here setter is internally triggered so Aurelia notified about object value change.
    // Without plugin that will not work and Aurelia will not be able to identify object change.
    this.timestamp.setHours(hours);
    this.timestamp.setMinutes(minutes);
  }
}

Configuration

Plugin options could be passed like below:

aurelia.use.plugin('aurelia-date-observer', {
  observeMode: 'property' // setter (default) | property
});

Available options are:

  • observeMode: Observe mode: "property" or "setter" (default)

Building The Code

To build the code, follow these steps.

  1. Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.

  2. From the project folder, execute the following command:

npm install
  1. Ensure that Gulp is installed. If you need to install it, use the following command:
npm install -g gulp
  1. To build the code, you can now run:
gulp build
  1. You will find the compiled code in the dist folder, available in module formats: AMD, CommonJS, ES2015, SystemJS.

aurelia-date-observer's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.