npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-observe

v1.0.4

Published

Backbone model implement which support observe features via Object.observe or Object.prototype.watch etc.

Downloads

10

Readme

NPM version NPM downloads MIT License Build Status

backbone-observe


Backbone-observe is a Backbone extension component which make Backbone framework to support Object.observe feature. This component can be used at browser and node-js environment.

What is Object.observe

Object.observe(), part of a future ECMAScript standard, is a method for asynchronously observing changes to JavaScript objects without the need for a separate library. It allows an observer to receive a time-ordered sequence of change records which describe the set of changes which took place to a set of observed objects. The Object.observe is a new technology which's going to change everything you think you know about data-binding. It’s also going to change how many of your MVC libraries approach observing models for edits and updates.

You can get more information in this article.

How to install

Node.js

Available in Node >= 0.11.13, the standards compliant Object.observe treasure resides.

npm install backbone-observe -g

And then require the package in your code.

var BackboneObserve = require("backbone-observe");
var om = new BackboneObserve.model({id: 'id', foo: 1, bar: 2, baz: 3});

AMD

require.config({
    path :{
        backbone : "you/backbone/path",
        underscore : "you/underscore/path",
        "backbone-observe" : "you/backbone-observe/path"
        //...code...
    }
});

require(["backbone-observe"], function(BackboneObserve){
    var ooModel = BackboneObserve.model;
    //...code...
});

Browser

<!-- Import backbone -->
<script type="text/javascript" src="underscore.min.js"></script>
<script type="text/javascript" src="backbone.min.js"></script>
<!-- Import Backbone-Observe -->
<script type="text/javascript" src="backbone-observe.js"></script>

<script type="text/javascript">
    var ooModel = Backbone.Observe.model;
    //...code...
</script>

How to use?

Every thing are same as Backbone documents except the processing of changing value at Backbone.Model is an asynchronous procedure. It means that you can not expect the value has changed after you invoke the set or unset function. Other important thing is the change events will not triggered in order because the Object.observe feature.

Here is an example:

var a = new Backbone.Observe.model({id: 'id', foo: 1, bar: 2, baz: 3});
a.on("change:foo", function(model, val) {
    console.log(a.get('foo')); //2
});
a.set({'foo': 2});
console.log(a.get('foo')); //1

We print the value of foo after set the value immediately. As you see, the value not be changed in this time but will come into effect at the callback of change event.So, the modify functiuon is totally an asynchronous operation.

In fact, the attributes of model will be observed by your browser and invoke the callback function for a period of time. Another example can help you to understand the mechanism of this action.

var changed = 0;
var obj = new Backbone.Observe.model();
obj.on('change', function() {
    changed += 1;
    console.log("Trigger change!");
});
obj.set({id: 1, label: 'c'});
setTimeout(function(){
    //output after the callback of change
    console.log(changed); //1
}, 0);

As you see, the callback of change will be executed before the timeout callback. This example reveals that the Object.observe will deliver the modify event and insert it to the event queue as soon as possible and excute it on the nearest timer tick.

If you not understand the event queue and the non-block I/O, you can see the follow documents.

Compatibility

  • Now the Google Chrome browser (version greater than 36) and the Chrome for Android has supported the Object.observe. You can check the MDN for more information.
  • The object.prototype.watch are supported by the Fiefox but not recommend to use.
  • The Node.js has supported the Object.observe in 0.11.13 but not publish to the standard version yet.

Benchmark test

You can run the benchmark test in here.Please make sure to use the browser supported to Object.observe feature.

Benchmark test recommend to expose Java’s nanosecond timer or enable Chrome’s microsecond timer, please read the documents of benchmarkjs for more information.

The follow is one test result in our computer: CPU: Intel Core i7 RAM: 4GB Chrome version: 39.0.2171.95

Backbone.set#test x 76,170 ops/sec ±11.36% (79 runs sampled)
Backbone.observe.set#test x 140,429 ops/sec ±2.66% (74 runs sampled)
Backbone.unset#test x 74,966 ops/sec ±1.95% (80 runs sampled)
Backbone.observe.set#test x 90,667 ops/sec ±2.77% (75 runs sampled)
Fastest is Backbone.observe.set#test
Fastest is Backbone.observe.unset#test

You can run the node-benchmark.js in test directory if you want to run benchmark in node.js.

Change Log

v1.0.4

Update Readme

v1.0.3

Add benchmarkjs test

v1.0.2

Release the project