GithubHelp home page GithubHelp logo

ember-auto's Introduction

Ember Auto Build Status

If you've used ember for a while, you'll be tired of writing this.get("property") everywhere in your code. E.g. from emberjs.com:

gravatarUrl: function() {
  var email = this.get('email'),
      size = this.get('size');

  return 'http://www.gravatar.com/avatar/' + hex_md5(email) + '?s=' + size;
}.property('email', 'size')

Wouldn't it be nice if instead of using strings to reference properties, they were just regular function arguments?

The Ember Auto Way

gravatarUrl: function(email, size) {
  return 'http://www.gravatar.com/avatar/' + hex_md5(email) + '?s=' + size;
}.auto("email", "size")

Or in coffeescript:

gravatarUrl: Em.Auto "email", "size", (email, size)->
  'http://www.gravatar.com/avatar/' + hex_md5(email) + '?s=' + size;

Use It

Just include ember-auto.js however you like:

<script type="text/javascript" src="ember.js"></script>
<script type="text/javascript" src="ember-auto.js"></script>

Is It Good?

Yes.

How Does it Work?

Ember Auto works by injecting the property's keys into it's function:

var Person = Em.Object.extend({
  name: "Richard",
  message: function (name, loadedAt) {
    return "Hi " + name + ", you've been here since " + loadedAt;
  }.auto("App.loadedAt", "name")
});

There are many ways to set the properties, if func is a function to turn into an auto property, these are all valid:

  • Em.computed("prop.path", func).auto()
  • Em.auto("prop.path", func)
  • Em.auto(func).property("prop.path")
  • func.auto("prop.path")
  • func.property("prop.path").auto()

What about special keys?

Computed properties in ember can reference special keys like .@auto and .[]. The last non-special segment is what's passed through:

var World = Em.Object.extend({
  continents: [
    { name: "Africa"      population: 1022234000 },
    { name: "America"     population: 934611000 },
    { name: "Antarctica"  population: 4490 },
    { name: "Australasia" population: 29127000 },
    { name: "Asia"        population: 4164252000 },
    { name: "Europe"      population: 738199000 }
  ],

  totalPopulation: function (continents) {
    return continents.reduce(function(total, c) {
      return total + c.population;
    }, 0);
  }.auto("[email protected]")
});

Contribute

Ember Auto wants to stay light. If you think you can enhance it, please do! Improvements to this readme would be particularly appreciated.

Ember Auto uses node.js and grunt as a build system, these two libraries will need to be installed before starting.

Setup

git clone https://github.com/gunn/ember-auto.git
cd ember-auto
npm install

Build Ember Auto

grunt

Unminified and minified builds will be placed in the dist directory.

Run Unit Tests

To setup:

npm install -g bower
npm install -g grunt-cli
bower install

Then run grunt test to execute the test suite headlessly via phantomjs, or grunt develop to run tests in a browser - tests are available at http://localhost:8000/tests

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.