Skip to content

Instantly share code, notes, and snippets.

Avatar

Jason Stone rolaveric

View GitHub Profile
@rolaveric
rolaveric / falcorCall.js
Last active Sep 8, 2015
Example of using falcor.Model.call()
View falcorCall.js
class MyController {
// ... snip
addTodoClicked() {
// Calls 'todos.add()' with the formModel
this.model.call(['todos', 'add'], [this.formModel])
.subscribe(_ => this.$scope.$evalAsync());
// Clear the form
this.formModel = {};
}
@rolaveric
rolaveric / falcorGetterSetter.html
Created Sep 6, 2015
HTML template for falcorGetterSetter.js
View falcorGetterSetter.html
<dl>
<dt>Title</dt>
<dd>{{:: ctrl.viewValue('titlesById[0].title')}}</dd>
<dt>Description</dt>
<dd><input type="text" ng-model="ctrl.getterSetter('titlesById[0].description')" ng-model-options="{getterSetter: true}"/></dd>
</dl>
@rolaveric
rolaveric / falcorGetterSetter.js
Last active Sep 8, 2015
getterSetter generator function used in Falcor article
View falcorGetterSetter.js
class MyController {
// ... snip
getterSetter(path) {
// Need to return an unbound function
var ctrl = this;
return function (newValue) {
// If arguments.length === 1 then the function is used as a setter,
// otherwise it's a getter
return arguments.length
// setValue() returns an Observable.
@rolaveric
rolaveric / falcorPageRange.html
Created Sep 2, 2015
Template which uses the pageRange filter
View falcorPageRange.html
<ul>
<li ng-repeat="index in ctrl.viewValue('titlesById.length') | pageRange:ctrl.page:ctrl.pageSize">
<dl>
<dt>Title</dt>
<dd>{{:: ctrl.viewValue('titlesById[' + index + '].title')}}</dd>
<dt>Description</dt>
<dd>{{:: ctrl.viewValue('titlesById[' + index + '].description')}}</dd>
</dl>
</li>
</ul>
@rolaveric
rolaveric / falcorPageRange.js
Created Sep 2, 2015
Example of a pageRange filter that can be used with FalcorJS to handle a range of values.
View falcorPageRange.js
function pageRangeFilterProvider() {
return function pageRangeFilter(itemCount, currentPage, pageSize) {
itemCount = itemCount || 0; // The 'length' of the collection
currentPage = currentPage || 1; // The current page number being viewed
pageSize = pageSize || 5; // The size of each page
const range = [];
for (var x = (currentPage - 1) * pageSize; x < currentPage * pageSize && x < itemCount; x++) {
range.push(x);
}
return range;
@rolaveric
rolaveric / falcorViewValue.html
Last active Sep 2, 2015
View template using viewValue() method, for FalcorJS + AngularJS v1.x post.
View falcorViewValue.html
<dl>
<dt>Title</dt>
<dd>{{:: ctrl.viewValue('titlesById[0].title')}}</dd>
<dt>Description</dt>
<dd>{{:: ctrl.viewValue('titlesById[0].description')}}</dd>
</dl>
View falcorViewValue.js
class MyController {
// ... snip
viewValue(path) {
// getCache() returns a JSON fragment, so we need to parse the path
const value = this.$parse(path)(this.model.getCache(path));
if (typeof value !== 'undefined') {
return typeof value === 'object' ? value.value : value;
}
// Cache miss - request the real value
View falcorSafeApply.js
// snip ...
class MyController {
// snip...
getValue() {
this.model.getValue('titlesById[523].name')
.subscribe(value => {
this.value = value;
this.$scope.$evalAsync();
});
@rolaveric
rolaveric / falcorModel.js
Last active Sep 2, 2015
A FalcorJS Model with a HttpDataSource loaded into an AngularJS controller
View falcorModel.js
import angular from 'angular';
import falcor from 'falcor';
import HttpDataSource from 'falcor-http-datasource';
class MyController {
constructor() {
this.model = new falcor.Model({
source: new HttpDataSource('/model.json')
}).batch(); // Batches value requests together
}
@rolaveric
rolaveric / falcorJsonGraph.js
Created Sep 2, 2015
JSON Graph sample for FalcorJS post
View falcorJsonGraph.js
{
todosById: {
"44": {
name: "get milk from corner store",
done: false,
prerequisites: [{ $type: "ref", value: ["todosById", 54] }]
},
"54": {
name: "withdraw money from ATM",
done: false,
You can’t perform that action at this time.