(Reduce)Computed


functional big data transclusion in the browser

@jonnii




What is the best

feature of ember?




Routing?


Maybe...




Ember.Select?


Hahahahahaha. NO.




Computed Properties!!


Seriously, they are magic.




You know you can do this...

http://jsfiddle.net/NQKvy/319/




Did you know you can do this?

http://jsfiddle.net/BvsEY/2/

Computed Properties just reduced.

This is how I discovered reduce computed...




What is reduce computed?


A reduce computed is a computed property which
observes the state of an array and reduces it 
into a single value...

e.g.

Ember.computed.min
Ember.computed.max

It's the reduce part of map reduce. a.k.a Big Data.


Reduce Computed

  • initialValue
  • options
  • addedItem
  • removedItem

 Ember.computed.max = function (dependentKey) {
  return Ember.reduceComputed.call(null, dependentKey, {
    initialValue: -Infinity,

    addedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
      return Math.max(accumulatedValue, item);
    },

    removedItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
      if (item < accumulatedValue) {
        return accumulatedValue;
      }
    }
  });
};

The secret sauce...


xxItem: function (accumulatedValue, item, changeMeta, instanceMeta) {
  ... magic ...    
},

changeMeta
index
item
arrayChanged
property


instanceMeta
A scratch pad for storing 
reduce computed state

Example Sum/Avg


http://jsfiddle.net/NQKvy/326/

Array Computed


Same as reduce computed except:

  • Result is an array
  • Result is kept up to date

For:

  • Filtering
  • Sorting

Group Example


http://jsfiddle.net/d82yd/5/

What next?


https://github.com/jamesotron/ember-enumerology
https://github.com/jamesarosen/ember-cpm

Feature actively being worked on
Someone please rewrite Ember.Select

Plugs


Code Now
http://codenow.org/

I'm hiring... 
Made with Slides.com