Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Random musings on "Scope", two-way live binding, and can.component

$
0
0
Recently, I just finished getting all mustache tests to pass with a new "Scope" property lookup module. Here's the branch: https://github.com/bitovi/canjs/tree/mustache-scope-manipulation. This is the first step, and possibly the most difficult, step in supporting two-way binding and can.component type features.

Before going further, I'd like to talk about "Scope" and get suggestions for naming it, it's API, etc. Here's Scope:



"Scope" is a terrible name.  It's more similar to how JavaScript's DOT operator walks up the proto-chain to find property values on "parent" objects or JavaScript's closure walks up call-objects/closures to find variable names.  Here's an example:


  1. var me = {name: {first: "Justin", last: "Meyer"}, age: 30}
  2. meScoped = new Scope(me)
  3. nameScoped = me.add(me.name);

  4. nameScoped.attr('first') //-> "Justin"
  5. nameScoped.attr('age') //-> 30

Notice how nameScoped.attr('age') will 

  1. look for properties on me.name, not find it 
  2. go to it's parent scope (meScoped)
  3. return meScoped's age property
Basically, scopes walk up to parent scopes looking for a property value. 

Scopes will have the same API as can.Observe.  You will be able to use .attr, .bind, etc.

Scopes will be a big part of can.component as they will be the View-Model.  For example, a template like:

<combo value="{{person.name}}" values="{{people}}"/>

Rendered with:

  1. var person = new Person(),
  2.      people = new Person.List({})
  3. template({
  4.   person: person,
  5.   people: people
  6. })

Will create a scope object like:

  1. templateScope =new Scope({
  2.   person: person,
  3.   people: people
  4. })

And the scope object created for the Combo can.component will look like:

  1. comboScope = templateScope.add({
  2.   value: person.compute('name')
  3.   values: people
  4. })

A few random thoughts / questions for people who are following along:

  • What should "Scope" be called?  It's a horrible name.
  • Should can.component inherit from "Scope"?  This would allow you to change value like: this.attr('value', "John"). Or would it be better to do something like this.viewModel.attr("value","John").







Viewing all articles
Browse latest Browse all 3491

Trending Articles