Is constant polling the way Aurelia's change detection is working?

江枫思渺然 提交于 2019-12-24 12:18:14

问题


I got div with if.bind working as was suggested in this question: Using literal JavaScript values in an Aurelia view. But then I noticed that showTemplate on viewModel is being constantly checked by framework, about 10 times per second. The stack is following:

execute._prototypeProperties.visible.get (welcome.js:29)
getValue (dirty-checking.js:93)
isDirty (dirty-checking.js:127)
check (dirty-checking.js:63)
(anonymous function) (dirty-checking.js:49)

Is it supposed to be like this? Seems to be not very resource-friendly.

Best regards, Eugene.


回答1:


Aurelia uses Object.observe for simple Javascript properties. If showTemplate is a function or a getter/setter, then Aurelia currently reverts to dirty checking. This can be removed by declaring the dependencies of the function. This is outlined here: https://github.com/aurelia/binding/pull/41

I have created a version of the Aurelia Skeleton project that implements this: https://github.com/ashleygrant/skeleton-navigation/tree/declare_dependencies

To implement this, you must switch to using the aurelia-main attribute. Add a main.js file:

import {LogManager} from 'aurelia-framework';
import {ConsoleAppender} from 'aurelia-logging-console';
import {ComputedObservationAdapter, ObjectObservationAdapter} from 'aurelia-framework';

LogManager.addAppender(new ConsoleAppender());
LogManager.setLevel(LogManager.levels.debug);

export function configure(aurelia) {
  aurelia.use
    .defaultBindingLanguage()
    .defaultResources()
    .router()
    .eventAggregator();

    aurelia.container
      .registerSingleton(ObjectObservationAdapter, ComputedObservationAdapter);

  aurelia.start().then(a => a.setRoot('app', document.body));
}

Then, in welcome.js, add the following import statement:

import {declarePropertyDependencies} from 'aurelia-framework';

and then outside the Welcome class, add the following method call:

declarePropertyDependencies(Welcome, 'fullName', ['firstName', 'lastName']);



来源:https://stackoverflow.com/questions/29028354/is-constant-polling-the-way-aurelias-change-detection-is-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!