Skip to content

Instantly share code, notes, and snippets.

View rolaveric's full-sized avatar

Jason Stone rolaveric

View GitHub Profile
@rolaveric
rolaveric / falcorCall.js
Last active September 8, 2015 10:49
Example of using falcor.Model.call()
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 September 6, 2015 07:23
HTML template for falcorGetterSetter.js
<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 September 8, 2015 10:52
getterSetter generator function used in Falcor article
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 September 2, 2015 12:41
Template which uses the pageRange filter
<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 September 2, 2015 12:38
Example of a pageRange filter that can be used with FalcorJS to handle a range of values.
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 September 2, 2015 12:41
View template using viewValue() method, for FalcorJS + AngularJS v1.x post.
<dl>
<dt>Title</dt>
<dd>{{:: ctrl.viewValue('titlesById[0].title')}}</dd>
<dt>Description</dt>
<dd>{{:: ctrl.viewValue('titlesById[0].description')}}</dd>
</dl>
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
// 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 September 2, 2015 11:58
A FalcorJS Model with a HttpDataSource loaded into an AngularJS controller
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 September 2, 2015 10:14
JSON Graph sample for FalcorJS post
{
todosById: {
"44": {
name: "get milk from corner store",
done: false,
prerequisites: [{ $type: "ref", value: ["todosById", 54] }]
},
"54": {
name: "withdraw money from ATM",
done: false,