Giter Club home page Giter Club logo

knockoutfire's Introduction

knockoutFire

KnockoutFire map Firebase json structure into HTML structure using KnockoutJs.

Live demo at jsFiddle.

How to use

Github pages as CDN

Latest release may have breaking changes.

I recommend to use versioned release, like 0.0.4 instead.

<script type="text/javascript" src="//hiroshi.github.io/knockoutFire/knockoutfire-0.0.4.js"></script>

Also see CHANGELOG.

Example

Firebase

items: {
  -XXX: {
    content: "Hello"
  }
  -YYY: {
    content: "World."
  }
}

HTML

<div id="viewModel">
  <ul data-bind="foreach: items">
    <li data-bind="text: content"></li>
  </ul>
</div>

Javascript

var firebase = new Firebase("https://knockoutFire-README-example.firebaseio-demo.com");
var viewModel = KnockoutFire.observable(firebase, {
  items: {
    "$item": {
      content: true,
    }
  }
});
ko.applyBindings(viewModel, document.getElementById("viewModel"));

API Reference

KnockoutFire.observable(firebaseRef, map)

The map option

The notation resembles to Firebase Security Rule.

Named propertiy

You need to specify which properties are used as observable properties of view models. KnockoutFire will retrieve only what specified in the map.

  • Each properties will be a ko.observable() and synchronized with the corresponding value in Firebase.
person: {
  firstName: true,
  lastName: true
}
$Variables and .reverse

If you use a property name start with $, the parent property will be ko.observableArray().

users: {
  "$user": {
    nickname: true,
  }
}

For add/remove/move operations, you should use Firebase API instead of manipulating observable array directly.

users()[1]().firebase.remove();

If you need reverse order;

comments: {
  ".reverse": true,
  "$comment": {
    content: true
  }
}
.startAt, .endAt, .limit
comments: {
  ".startAt": 0,
  ".endAt": {
    ".priority": 100,
    ".name": "foo"
  },
  ".limit": 20
}

Querying and Limiting Data in Firebase | Firebase Documentation

.newItem and .priority

.newItem adds additional sub viewModel to an observable array.

comments: {
  ".newItem": true,
  "$comment": {
    content: true
  }
}
<div data-bind="with: comments.newItem">
  <form data-bind="submit: create">
    <input type="text" data-bind="value: content">
    <input type="submit">
  </form>
</div>

If you need a priority to be set;

".newItem": {
  ".priority": function() { return Date.now() }
}

If you need a default value rather than from a data-bind;

".newItem": {
  isdone: function(){ return false; }
}

If you need a callback on success;

".newItem": {
  ".on_success": function(){ do_someting(); }
}
.indexOf

To use Denormalized data use .indexOf.

members: {
  "$user": {
    ".indexOf": "/users/$user",
    "nickName": true
  }
}

You can access nickName like this:

members()[0]().nickName()
.extend

You can use Knockout extender.

ko.extenders.person = function(self, option) {
  self.fullName = ko.computed(function() {
    return this.firstName() + " " + this.lastName();
  });
};

Then specify the extender by name:

person: {
  firstName: true,
  lastName: true,
  ".extend": {person: true}
}

knockoutfire's People

Contributors

hiroshi avatar ayurmedia avatar

Watchers

James Cloos avatar  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.