greim / ng-falcor

Use Falcor in Angular 1.x Projects
MIT License
14 stars 6 forks source link

ng-falcor: Use Falcor in Angular 1.x Projects

Installation

npm install ng-falcor

Alternatively, copy the UMD file dist/ng-falcor.js in this repo and put it wherever you want.

Experimental!

This lib is pre-1.0 and likely have breaking changes before hitting 1.0, especially as Falcor (which is also pre 1.0) continues to evolve.

How does it work?

See the Falcor website for how Falcor works. This lib provides an Angular factory that wraps a Falcor model and exposes it to your logic and templates. Subsequently, Angular bindings operate against a single source of truth consisting of a Falcor model containing all your application data. From then on, it's simply a matter of manipulating the JSON graph via the model.

API

ngFalcor.create(opts)

The main export of ng-falcor has but one method create() which returns an Angular factory function. Pass it an options hash:

ngf

This is the singleton object that gets injected into your controllers by the factory. You can name it whatever you want, but ngf is nice and short. It has several methods:

Iteration helpers

Often your JSON graph will contain lists, which you'll iterate using ng-repeat. This lib offers some helper utilities.

Iteration mode 1: simple ranges

ngf.range(lo, hi) returns an array of integers from lo to hi (inclusive). This list can be passed directly to ng-repeat and each integer can then be used as an index into your Falcor list. See the recipes section for examples.

Iteration mode 2: stepping pagers

A stepping pager supports the traditional paging scenario. It's an object gotten by calling ngf.stepper(size), where size is the number of things on each page. See the recipes section for examples. A stepping pager has these methods:

Iteration mode 3: increasing pagers

An increasing pager supports the infinite scrolling scenario. It's an object gotten by calling ngf.increaser(size), where size is the number of things to increase by each time. See the recipes section for examples. An increasing pager has these methods:

Credit

Credit due to @rolaveric for inspiration and providing some useful pieces to this puzzle. And of course the Falcor and Angular teams.