setPriority
Provide a priority for when an observable that derives its value should be re-evaluated.
setPriority(obj, priority)
Calls an underlying @can.setPriority
symbol on obj
if it exists with priorty
.
Returns true
if a priority was set, false
if otherwise.
Lower priorities (0
being the lowest), will be an indication to run earlier than
higher priorities.
var obj = canReflect.assignSymbols({},{
"can.setPriority": function(priority){
return this.priority = priority;
}
});
canReflect.setPriority(obj, 0) //-> true
obj.priority //-> 0
canReflect.setPriority({},20) //-> false
Parameters
- obj
{Object}
:An observable that will update its priority.
- priority
{Number}
:The priority number. Lower priorities (
0
being the lowest), indicate to run earlier than higher priorities.
Returns
{Boolean}
:
true
if a priority was able to be set, false
if otherwise.
Use
There's often a need to specify the order of re-evaluation for observables that derive (or compute) their value from other observables.
This is needed by templates to avoid unnecessary re-evaluation. Say we had the following template:
{{#if value}}
{{value}}
{{/if}}
If value
became falsey, we'd want the {{#if}}
to be aware of it before
the {{value}}
magic tags updated. We can do that by setting priorities:
canReflect.setPriority(magicIfObservable, 0);
canReflect.setPriority(magicValueObservable,1);
Internally, those observables will use that priority
to register their
re-evaluation with the derive
queue in [can-queues].