├── app └── .gitkeep ├── vendor └── .gitkeep ├── tests ├── dummy │ ├── app │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── models │ │ │ └── .gitkeep │ │ ├── routes │ │ │ └── .gitkeep │ │ ├── components │ │ │ └── .gitkeep │ │ ├── templates │ │ │ ├── components │ │ │ │ └── .gitkeep │ │ │ └── application.hbs │ │ ├── resolver.js │ │ ├── styles │ │ │ └── app.css │ │ ├── router.js │ │ ├── app.js │ │ ├── index.html │ │ └── controllers │ │ │ └── application.js │ ├── config │ │ ├── optional-features.json │ │ ├── targets.js │ │ └── environment.js │ ├── public │ │ ├── robots.txt │ │ └── crossdomain.xml │ └── .jshintrc ├── helpers │ ├── destroy-app.js │ ├── resolver.js │ ├── start-app.js │ └── module-for-acceptance.js ├── test-helper.js ├── .jshintrc ├── index.html └── unit │ └── change-gate-test.js ├── .watchmanconfig ├── .template-lintrc.js ├── index.js ├── config ├── environment.js └── ember-try.js ├── .ember-cli ├── .eslintignore ├── .editorconfig ├── .gitignore ├── .npmignore ├── .jshintrc ├── testem.js ├── ember-cli-build.js ├── CONTRIBUTING.md ├── LICENSE.md ├── .eslintrc.js ├── .travis.yml ├── package.json ├── addon └── change-gate.js └── README.md /app/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /tests/dummy/config/optional-features.json: -------------------------------------------------------------------------------- 1 | { 2 | "jquery-integration": false 3 | } 4 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /.template-lintrc.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | extends: 'recommended' 5 | }; 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | name: require('./package').name 5 | }; 6 | -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | margin: 20px; 3 | } 4 | 5 | input { width: 100%; } 6 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(/* environment, appConfig */) { 4 | return { }; 5 | }; 6 | -------------------------------------------------------------------------------- /tests/helpers/destroy-app.js: -------------------------------------------------------------------------------- 1 | import { run } from '@ember/runloop'; 2 | 3 | export default function destroyApp(application) { 4 | run(application, 'destroy'); 5 | } 6 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import Application from '../app'; 2 | import config from '../config/environment'; 3 | import { setApplication } from '@ember/test-helpers'; 4 | import { start } from 'ember-qunit'; 5 | 6 | setApplication(Application.create(config.APP)); 7 | 8 | start(); 9 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import EmberRouter from '@ember/routing/router'; 2 | import config from './config/environment'; 3 | 4 | const Router = EmberRouter.extend({ 5 | location: config.locationType, 6 | rootURL: config.rootURL 7 | }); 8 | 9 | Router.map(function() { 10 | }); 11 | 12 | export default Router; 13 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/application.hbs: -------------------------------------------------------------------------------- 1 |

ember-computed-change-gate

2 | 3 | Type some words: {{input value=text}} 4 | 5 |
6 | gatedWordCount: {{gatedWordCount}} | gatedObserverCount: {{gatedObserverCount}}
7 | normalWordCount: {{normalWordCount}} | normalObserverCount: {{normalObserverCount}}
8 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /tests/helpers/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from '../../resolver'; 2 | import config from '../../config/environment'; 3 | 4 | const resolver = Resolver.create(); 5 | 6 | resolver.namespace = { 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix 9 | }; 10 | 11 | export default resolver; 12 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # unconventional js 2 | /blueprints/*/files/ 3 | /vendor/ 4 | 5 | # compiled output 6 | /dist/ 7 | /tmp/ 8 | 9 | # dependencies 10 | /bower_components/ 11 | /node_modules/ 12 | 13 | # misc 14 | /coverage/ 15 | !.* 16 | 17 | # ember-try 18 | /.node_modules.ember-try/ 19 | /bower.json.ember-try 20 | /package.json.ember-try 21 | -------------------------------------------------------------------------------- /tests/dummy/config/targets.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const browsers = [ 4 | 'last 1 Chrome versions', 5 | 'last 1 Firefox versions', 6 | 'last 1 Safari versions' 7 | ]; 8 | 9 | const isCI = !!process.env.CI; 10 | const isProduction = process.env.EMBER_ENV === 'production'; 11 | 12 | if (isCI || isProduction) { 13 | browsers.push('ie 11'); 14 | } 15 | 16 | module.exports = { 17 | browsers 18 | }; 19 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Application from '@ember/application'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | const App = Application.extend({ 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix, 9 | Resolver 10 | }); 11 | 12 | loadInitializers(App, config.modulePrefix); 13 | 14 | export default App; 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.hbs] 17 | insert_final_newline = false 18 | 19 | [*.{diff,md}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist/ 5 | /tmp/ 6 | 7 | # dependencies 8 | /bower_components/ 9 | /node_modules/ 10 | 11 | # misc 12 | /.env* 13 | /.pnp* 14 | /.sass-cache 15 | /connect.lock 16 | /coverage/ 17 | /libpeerconnection.log 18 | /npm-debug.log* 19 | /testem.log 20 | /yarn-error.log 21 | 22 | # ember-try 23 | /.node_modules.ember-try/ 24 | /bower.json.ember-try 25 | /package.json.ember-try 26 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist/ 3 | /tmp/ 4 | 5 | # dependencies 6 | /bower_components/ 7 | 8 | # misc 9 | /.bowerrc 10 | /.editorconfig 11 | /.ember-cli 12 | /.env* 13 | /.eslintignore 14 | /.eslintrc.js 15 | /.gitignore 16 | /.template-lintrc.js 17 | /.travis.yml 18 | /.watchmanconfig 19 | /bower.json 20 | /config/ember-try.js 21 | /CONTRIBUTING.md 22 | /ember-cli-build.js 23 | /testem.js 24 | /tests/ 25 | /yarn.lock 26 | .gitkeep 27 | 28 | # ember-try 29 | /.node_modules.ember-try/ 30 | /bower.json.ember-try 31 | /package.json.ember-try 32 | -------------------------------------------------------------------------------- /tests/helpers/start-app.js: -------------------------------------------------------------------------------- 1 | import { run } from '@ember/runloop'; 2 | import { assign } from '@ember/polyfills'; 3 | import Application from '../../app'; 4 | import config from '../../config/environment'; 5 | 6 | export default function startApp(attrs) { 7 | let application; 8 | 9 | // use defaults, but you can override 10 | let attributes = assign({}, config.APP, attrs); 11 | 12 | run(() => { 13 | application = Application.create(attributes); 14 | application.setupForTesting(); 15 | application.injectTestHelpers(); 16 | }); 17 | 18 | return application; 19 | } 20 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "-Promise" 6 | ], 7 | "browser": true, 8 | "boss": true, 9 | "curly": true, 10 | "debug": false, 11 | "devel": true, 12 | "eqeqeq": true, 13 | "evil": true, 14 | "forin": false, 15 | "immed": false, 16 | "laxbreak": false, 17 | "newcap": true, 18 | "noarg": true, 19 | "noempty": false, 20 | "nonew": false, 21 | "nomen": false, 22 | "onevar": false, 23 | "plusplus": false, 24 | "regexp": false, 25 | "undef": true, 26 | "sub": true, 27 | "strict": false, 28 | "white": false, 29 | "eqnull": true, 30 | "esversion": 6, 31 | "unused": true 32 | } 33 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | test_page: 'tests/index.html?hidepassed', 3 | disable_watching: true, 4 | launch_in_ci: [ 5 | 'Chrome' 6 | ], 7 | launch_in_dev: [ 8 | 'Chrome' 9 | ], 10 | browser_args: { 11 | Chrome: { 12 | ci: [ 13 | // --no-sandbox is needed when running Chrome inside a container 14 | process.env.CI ? '--no-sandbox' : null, 15 | '--headless', 16 | '--disable-dev-shm-usage', 17 | '--disable-software-rasterizer', 18 | '--mute-audio', 19 | '--remote-debugging-port=0', 20 | '--window-size=1440,900' 21 | ].filter(Boolean) 22 | } 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /tests/dummy/public/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 4 | 5 | module.exports = function(defaults) { 6 | let app = new EmberAddon(defaults, { 7 | // Add options here 8 | 'ember-cli-babel': { 9 | optional: ['es6.spec.symbols'], 10 | includePolyfill: true 11 | } 12 | }); 13 | 14 | /* 15 | This build file specifies the options for the dummy test app of this 16 | addon, located in `/tests/dummy` 17 | This build file does *not* influence how the addon or the app using it 18 | behave. You most likely want to be modifying `./index.js` or app's build file 19 | */ 20 | 21 | return app.toTree(); 22 | }; 23 | -------------------------------------------------------------------------------- /tests/dummy/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": { 3 | "document": true, 4 | "window": true, 5 | "-Promise": true 6 | }, 7 | "browser" : true, 8 | "boss" : true, 9 | "curly": true, 10 | "debug": false, 11 | "devel": true, 12 | "eqeqeq": true, 13 | "evil": true, 14 | "forin": false, 15 | "immed": false, 16 | "laxbreak": false, 17 | "newcap": true, 18 | "noarg": true, 19 | "noempty": false, 20 | "nonew": false, 21 | "nomen": false, 22 | "onevar": false, 23 | "plusplus": false, 24 | "regexp": false, 25 | "undef": true, 26 | "sub": true, 27 | "strict": false, 28 | "white": false, 29 | "eqnull": true, 30 | "esnext": true, 31 | "unused": true 32 | } 33 | -------------------------------------------------------------------------------- /tests/helpers/module-for-acceptance.js: -------------------------------------------------------------------------------- 1 | import { Promise } from 'rsvp'; 2 | import { module } from 'qunit'; 3 | import startApp from '../helpers/start-app'; 4 | import destroyApp from '../helpers/destroy-app'; 5 | 6 | export default function(name, options = {}) { 7 | module(name, { 8 | beforeEach() { 9 | this.application = startApp(); 10 | 11 | if (options.beforeEach) { 12 | return options.beforeEach.apply(this, arguments); 13 | } 14 | }, 15 | 16 | afterEach() { 17 | let afterEach = options.afterEach && options.afterEach.apply(this, arguments); 18 | return Promise.resolve(afterEach).then(() => destroyApp(this.application)); 19 | } 20 | }); 21 | } 22 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How To Contribute 2 | 3 | ## Installation 4 | 5 | * `git clone ` 6 | * `cd my-addon` 7 | * `npm install` 8 | 9 | ## Linting 10 | 11 | * `npm run lint:hbs` 12 | * `npm run lint:js` 13 | * `npm run lint:js -- --fix` 14 | 15 | ## Running tests 16 | 17 | * `ember test` – Runs the test suite on the current Ember version 18 | * `ember test --server` – Runs the test suite in "watch mode" 19 | * `ember try:each` – Runs the test suite against multiple Ember versions 20 | 21 | ## Running the dummy application 22 | 23 | * `ember serve` 24 | * Visit the dummy application at [http://localhost:4200](http://localhost:4200). 25 | 26 | For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/application.js: -------------------------------------------------------------------------------- 1 | import { observer, computed } from '@ember/object'; 2 | import Controller from '@ember/controller'; 3 | import changeGate from 'ember-computed-change-gate/change-gate'; 4 | 5 | export default Controller.extend({ 6 | text: 'Jump over the gate', 7 | 8 | gatedObserverCount: 0, 9 | gatedWordCount: changeGate('text', function(value) { 10 | return value.trim().split(/\s+/).length; 11 | }), 12 | //eslint-disable-next-line ember/no-observers 13 | gatedWordCountChanged: observer('gatedWordCount', function() { 14 | this.incrementProperty('gatedObserverCount'); 15 | }), 16 | 17 | normalObserverCount: 0, 18 | normalWordCount: computed('text', function() { 19 | return this.get('text').trim().split(/\s+/).length; 20 | }), 21 | //eslint-disable-next-line ember/no-observers 22 | normalWordCountChanged: observer('normalWordCount', function() { 23 | this.incrementProperty('normalObserverCount'); 24 | }) 25 | }); 26 | -------------------------------------------------------------------------------- /tests/.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "predef": [ 3 | "document", 4 | "window", 5 | "location", 6 | "setTimeout", 7 | "$", 8 | "-Promise", 9 | "define", 10 | "console", 11 | "visit", 12 | "exists", 13 | "fillIn", 14 | "click", 15 | "keyEvent", 16 | "triggerEvent", 17 | "find", 18 | "findWithAssert", 19 | "wait", 20 | "DS", 21 | "andThen", 22 | "currentURL", 23 | "currentPath", 24 | "currentRouteName" 25 | ], 26 | "node": false, 27 | "browser": false, 28 | "boss": true, 29 | "curly": true, 30 | "debug": false, 31 | "devel": false, 32 | "eqeqeq": true, 33 | "evil": true, 34 | "forin": false, 35 | "immed": false, 36 | "laxbreak": false, 37 | "newcap": true, 38 | "noarg": true, 39 | "noempty": false, 40 | "nonew": false, 41 | "nomen": false, 42 | "onevar": false, 43 | "plusplus": false, 44 | "regexp": false, 45 | "undef": true, 46 | "sub": true, 47 | "strict": false, 48 | "white": false, 49 | "eqnull": true, 50 | "esversion": 6, 51 | "unused": true 52 | } 53 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2019 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {{content-for "body-footer"}} 31 | {{content-for "test-body-footer"}} 32 | 33 | 34 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parserOptions: { 4 | ecmaVersion: 2018, 5 | sourceType: 'module' 6 | }, 7 | plugins: [ 8 | 'ember' 9 | ], 10 | extends: [ 11 | 'eslint:recommended', 12 | 'plugin:ember/recommended' 13 | ], 14 | env: { 15 | browser: true 16 | }, 17 | rules: { 18 | }, 19 | overrides: [ 20 | // node files 21 | { 22 | files: [ 23 | '.eslintrc.js', 24 | '.template-lintrc.js', 25 | 'ember-cli-build.js', 26 | 'index.js', 27 | 'testem.js', 28 | 'blueprints/*/index.js', 29 | 'config/**/*.js', 30 | 'tests/dummy/config/**/*.js' 31 | ], 32 | excludedFiles: [ 33 | 'addon/**', 34 | 'addon-test-support/**', 35 | 'app/**', 36 | 'tests/dummy/app/**' 37 | ], 38 | parserOptions: { 39 | sourceType: 'script' 40 | }, 41 | env: { 42 | browser: false, 43 | node: true 44 | }, 45 | plugins: ['node'], 46 | rules: Object.assign({}, require('eslint-plugin-node').configs.recommended.rules, { 47 | // add your custom rules and overrides for node files here 48 | }) 49 | } 50 | ] 51 | }; 52 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | # we recommend testing addons with the same minimum supported node version as Ember CLI 5 | # so that your addon works for all apps 6 | - "8" 7 | 8 | sudo: false 9 | dist: trusty 10 | 11 | addons: 12 | chrome: stable 13 | 14 | cache: 15 | directories: 16 | - $HOME/.npm 17 | 18 | env: 19 | global: 20 | # See https://git.io/vdao3 for details. 21 | - JOBS=1 22 | 23 | branches: 24 | only: 25 | - master 26 | # npm version tags 27 | - /^v\d+\.\d+\.\d+/ 28 | 29 | jobs: 30 | fail_fast: true 31 | allow_failures: 32 | - env: EMBER_TRY_SCENARIO=ember-canary 33 | 34 | include: 35 | # runs linting and tests with current locked deps 36 | 37 | - stage: "Tests" 38 | name: "Tests" 39 | script: 40 | - npm run lint:hbs 41 | - npm run lint:js 42 | - npm test 43 | 44 | # we recommend new addons test the current and previous LTS 45 | # as well as latest stable release (bonus points to beta/canary) 46 | - stage: "Additional Tests" 47 | env: EMBER_TRY_SCENARIO=ember-lts-3.4 48 | - env: EMBER_TRY_SCENARIO=ember-lts-3.8 49 | - env: EMBER_TRY_SCENARIO=ember-release 50 | - env: EMBER_TRY_SCENARIO=ember-beta 51 | - env: EMBER_TRY_SCENARIO=ember-canary 52 | - env: EMBER_TRY_SCENARIO=ember-default-with-jquery 53 | 54 | script: 55 | - node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO 56 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function(environment) { 4 | let ENV = { 5 | modulePrefix: 'dummy', 6 | environment, 7 | rootURL: '/', 8 | locationType: 'auto', 9 | EmberENV: { 10 | FEATURES: { 11 | // Here you can enable experimental features on an ember canary build 12 | // e.g. EMBER_NATIVE_DECORATOR_SUPPORT: true 13 | }, 14 | EXTEND_PROTOTYPES: { 15 | // Prevent Ember Data from overriding Date.parse. 16 | Date: false 17 | } 18 | }, 19 | 20 | APP: { 21 | // Here you can pass flags/options to your application instance 22 | // when it is created 23 | } 24 | }; 25 | 26 | if (environment === 'development') { 27 | // ENV.APP.LOG_RESOLVER = true; 28 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 29 | // ENV.APP.LOG_TRANSITIONS = true; 30 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 31 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 32 | } 33 | 34 | if (environment === 'test') { 35 | // Testem prefers this... 36 | ENV.locationType = 'none'; 37 | 38 | // keep test console output quieter 39 | ENV.APP.LOG_ACTIVE_GENERATION = false; 40 | ENV.APP.LOG_VIEW_LOOKUPS = false; 41 | 42 | ENV.APP.rootElement = '#ember-testing'; 43 | ENV.APP.autoboot = false; 44 | } 45 | 46 | if (environment === 'production') { 47 | // here you can enable a production-specific feature 48 | } 49 | 50 | return ENV; 51 | }; 52 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const getChannelURL = require('ember-source-channel-url'); 4 | 5 | module.exports = async function() { 6 | return { 7 | scenarios: [ 8 | { 9 | name: 'ember-lts-3.4', 10 | npm: { 11 | devDependencies: { 12 | 'ember-source': '~3.4.0' 13 | } 14 | } 15 | }, 16 | { 17 | name: 'ember-lts-3.8', 18 | npm: { 19 | devDependencies: { 20 | 'ember-source': '~3.8.0' 21 | } 22 | } 23 | }, 24 | { 25 | name: 'ember-release', 26 | npm: { 27 | devDependencies: { 28 | 'ember-source': await getChannelURL('release') 29 | } 30 | } 31 | }, 32 | { 33 | name: 'ember-beta', 34 | npm: { 35 | devDependencies: { 36 | 'ember-source': await getChannelURL('beta') 37 | } 38 | } 39 | }, 40 | { 41 | name: 'ember-canary', 42 | npm: { 43 | devDependencies: { 44 | 'ember-source': await getChannelURL('canary') 45 | } 46 | } 47 | }, 48 | // The default `.travis.yml` runs this scenario via `npm test`, 49 | // not via `ember try`. It's still included here so that running 50 | // `ember try:each` manually or from a customized CI config will run it 51 | // along with all the other scenarios. 52 | { 53 | name: 'ember-default', 54 | npm: { 55 | devDependencies: {} 56 | } 57 | }, 58 | { 59 | name: 'ember-default-with-jquery', 60 | env: { 61 | EMBER_OPTIONAL_FEATURES: JSON.stringify({ 62 | 'jquery-integration': true 63 | }) 64 | }, 65 | npm: { 66 | devDependencies: { 67 | '@ember/jquery': '^0.5.1' 68 | } 69 | } 70 | } 71 | ] 72 | }; 73 | }; 74 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-computed-change-gate", 3 | "version": "1.1.0", 4 | "description": "Create computed properties which trigger observers only if the value of the computed property has changed", 5 | "keywords": [ 6 | "ember-addon" 7 | ], 8 | "repository": "https://github.com/GavinJoyce/ember-computed-change-gate", 9 | "license": "MIT", 10 | "author": "Gavin Joyce", 11 | "directories": { 12 | "doc": "doc", 13 | "test": "tests" 14 | }, 15 | "scripts": { 16 | "build": "ember build", 17 | "lint:hbs": "ember-template-lint .", 18 | "lint:js": "eslint .", 19 | "start": "ember serve", 20 | "test": "ember test", 21 | "test:all": "ember try:each" 22 | }, 23 | "dependencies": { 24 | "ember-cli-babel": "^7.7.3" 25 | }, 26 | "devDependencies": { 27 | "@ember/optional-features": "^0.7.0", 28 | "broccoli-asset-rev": "^3.0.0", 29 | "ember-cli": "~3.12.0", 30 | "ember-cli-app-version": "3.1.3", 31 | "ember-cli-dependency-checker": "^3.1.0", 32 | "ember-cli-eslint": "^5.1.0", 33 | "ember-cli-htmlbars": "^3.0.1", 34 | "ember-cli-htmlbars-inline-precompile": "^2.1.0", 35 | "ember-cli-inject-live-reload": "^1.8.2", 36 | "ember-cli-release": "^0.2.9", 37 | "ember-cli-sri": "^2.1.1", 38 | "ember-cli-template-lint": "^1.0.0-beta.1", 39 | "ember-cli-uglify": "^2.1.0", 40 | "ember-disable-prototype-extensions": "^1.1.3", 41 | "ember-export-application-global": "^2.0.0", 42 | "ember-load-initializers": "^2.0.0", 43 | "ember-qunit": "^4.4.1", 44 | "ember-resolver": "^5.0.1", 45 | "ember-source": "~3.12.0", 46 | "ember-source-channel-url": "^1.1.0", 47 | "ember-try": "^1.0.0", 48 | "eslint-plugin-ember": "^6.2.0", 49 | "eslint-plugin-node": "^9.0.1", 50 | "loader.js": "^4.7.0", 51 | "qunit-dom": "^0.8.4" 52 | }, 53 | "engines": { 54 | "node": "8.* || >= 10.*" 55 | }, 56 | "ember-addon": { 57 | "configPath": "tests/dummy/config" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /addon/change-gate.js: -------------------------------------------------------------------------------- 1 | import { isEqual } from '@ember/utils'; 2 | import { computed } from '@ember/object'; 3 | import { assert } from '@ember/debug'; 4 | 5 | export default function() { 6 | let args = [].slice.call(arguments); 7 | let filter = null; 8 | let config = null; 9 | 10 | let last = args[args.length-1]; 11 | if (typeof last === 'function') { 12 | filter = args.pop(); 13 | } else if (typeof last === 'object' && last.sync !== undefined) { 14 | let secondLast = args[args.length-2]; 15 | config = args.pop(); 16 | if (typeof secondLast === 'function') { 17 | filter = args.pop(); 18 | } 19 | } 20 | 21 | // no filter function 22 | if (!filter) { 23 | // passing a function is optional only for computeds with a single dependency 24 | let message = 'When depending on multiple properties a function must be passed as the last argument.'; 25 | assert(message, args.length === 1); 26 | } 27 | 28 | let dependentKeys = args; // for code read-ability 29 | 30 | function computeValue(dependentKeys) { 31 | let dependentValues = dependentKeys.map(dependentKey => { 32 | return this.get(dependentKey); 33 | }); 34 | 35 | if (!filter) { 36 | return dependentValues[0]; 37 | } 38 | 39 | return filter.apply(this, dependentValues); 40 | } 41 | 42 | let changeGateComputed = computed(function handler(key) { 43 | let lastValueKey = `__changeGate${key}LastValue`; 44 | 45 | function attemptPropertyChange(dependentKeys) { 46 | let newValue = computeValue.call(this, dependentKeys); 47 | let lastValue = this[lastValueKey]; 48 | 49 | if(!isEqual(newValue, lastValue)) { 50 | this[lastValueKey] = newValue; 51 | this.notifyPropertyChange(key); 52 | } 53 | } 54 | 55 | let isFirstRun = !this.hasOwnProperty(lastValueKey); 56 | if (isFirstRun) { 57 | this[lastValueKey] = computeValue.call(this, dependentKeys); 58 | 59 | //setup observers responsible for notifying property changes 60 | let handleDependencyChange = () => { 61 | return attemptPropertyChange.call(this, dependentKeys); 62 | }; 63 | for(let dependentKey of dependentKeys) { 64 | let params = [dependentKey, handleDependencyChange] 65 | if (config && config.sync !== undefined) { 66 | // We need to push `null` because the `addObserver` method signature is `addObserver(obj, path, method, target, sync)` 67 | params.push(null); 68 | params.push(config.sync); 69 | } 70 | this.addObserver(...params); 71 | } 72 | } 73 | 74 | return this[lastValueKey]; 75 | }); 76 | 77 | return changeGateComputed; 78 | } 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ember-computed-change-gate 2 | 3 | [![Build Status](https://travis-ci.org/GavinJoyce/ember-computed-change-gate.svg)](https://travis-ci.org/GavinJoyce/ember-computed-change-gate) 4 | 5 | [![Ember Observer Score](http://emberobserver.com/badges/ember-computed-change-gate.svg)](http://emberobserver.com/addons/ember-computed-change-gate) 6 | 7 | Observers on Ember.js computed properties are fired if a dependant key changes, regardless of whether the property value changes or not. `ember-computed-change-gate` only triggers observers when the result of a computed property changes. 8 | 9 | Consider the following example: 10 | 11 | ```javascript 12 | Ember.Object.extend({ 13 | name: 'Gavin', 14 | trimmedName: Ember.computed('name'), function() { 15 | return this.get('name').trim(); 16 | }), 17 | onTrimmedNameChanged: Ember.observer('trimmedName', function() { 18 | console.log('trimmedName changed'); 19 | }) 20 | }); 21 | ``` 22 | 23 | Every time `name` changes `onTrimmedNameChanged` will be run, even if the value of `trimmedName` doesn't change. 24 | 25 | ```javascript 26 | import changeGate from 'ember-computed-change-gate/change-gate'; 27 | 28 | Ember.Object.extend({ 29 | name: 'Gavin', 30 | trimmedName: changeGate('name', function(value) { 31 | return value.trim(); 32 | }), 33 | onTrimmedNameChanged: Ember.observer('trimmedName', function() { 34 | console.log('trimmedName changed'); 35 | }) 36 | }); 37 | ``` 38 | 39 | Using `changeGate` will prevent the `onTrimmedNameChanged` observer from firing unless the value of `trimmedName` changes. Please see the video below for an example of how I've used this when building [Intercom](https://www.intercom.io/): 40 | 41 | ## Advanced configuration 42 | Since Ember 3.11 extra configuration can be passed to observers to allow them to be configured synchronous or asynchronous. To configure the synchronous state of the observer in `changeGate` pass a config object as the last param with the `sync` property set appropriately. 43 | 44 | For example: 45 | 46 | ```js 47 | // synchronous observer 48 | trimmedName: changeGate('name', function(value) { 49 | return value.trim(); 50 | }, { sync: true }), 51 | 52 | //asynchronous observer 53 | trimmedName: changeGate('name', function(value) { 54 | return value.trim(); 55 | }, { sync: false }), 56 | ``` 57 | 58 | See [this RFC](https://emberjs.github.io/rfcs/0494-async-observers.html) and [blog post](https://www.pzuraq.com/ember-octane-update-async-observers/) for more informationa about async observers. 59 | 60 | 61 | ## Watch a screencast showing how this addon was built below 62 | 63 | [![Image](https://cloud.githubusercontent.com/assets/2526/4349867/d399b15e-41c9-11e4-8319-43c2e06186aa.png)](https://www.youtube.com/watch?v=PDgvMAyA8ic) 64 | 65 | Questions? Ping me [@gavinjoyce](https://twitter.com/gavinjoyce) 66 | 67 | ## Installation 68 | 69 | This is an Ember CLI addon, to install: 70 | 71 | `ember install ember-computed-change-gate` 72 | 73 | ## Development Instructions 74 | 75 | * `git clone` this repository 76 | * `npm install` 77 | * `bower install` 78 | 79 | ### Linting 80 | 81 | * `npm run lint:js` 82 | * `npm run lint:js -- --fix` 83 | 84 | ### Running tests 85 | 86 | * `ember test` – Runs the test suite on the current Ember version 87 | * `ember test --server` – Runs the test suite in "watch mode" 88 | * `ember try:each` – Runs the test suite against multiple Ember versions 89 | 90 | ### Running the dummy application 91 | 92 | * `ember serve` 93 | * Visit the dummy application at [http://localhost:4200](http://localhost:4200). 94 | 95 | For more information on using ember-cli, visit [https://ember-cli.com/](https://ember-cli.com/). 96 | 97 | License 98 | ------------------------------------------------------------------------------ 99 | 100 | This project is licensed under the [MIT License](LICENSE.md). -------------------------------------------------------------------------------- /tests/unit/change-gate-test.js: -------------------------------------------------------------------------------- 1 | import EmberObject, { computed } from '@ember/object'; 2 | import { module, test } from 'qunit'; 3 | import changeGate from 'ember-computed-change-gate/change-gate'; 4 | 5 | module('changeGate', function() { 6 | test('a changeGate with a function', function(assert) { 7 | var Paragraph = EmberObject.extend({ 8 | text: 'Hello there', 9 | wordCount: changeGate('text', function(value) { 10 | return value.split(/\s+/).length; 11 | }) 12 | }); 13 | 14 | var paragraph = Paragraph.create({ text: 'This is an interesting sentence' }); 15 | assert.equal(paragraph.get('wordCount'), '5'); 16 | 17 | var textObserverCount = 0; 18 | var wordCountObserverCount = 0; 19 | 20 | paragraph.addObserver('text', function() { 21 | textObserverCount++; 22 | }); 23 | 24 | paragraph.addObserver('wordCount', function() { 25 | wordCountObserverCount++; 26 | }); 27 | 28 | paragraph.set('text', 'This also has five words'); 29 | assert.equal(textObserverCount, 1); 30 | assert.equal(wordCountObserverCount, 0, 'the gated observer does not fire when the value does not change'); 31 | 32 | paragraph.set('text', 'This has four words'); 33 | assert.equal(textObserverCount, 2); 34 | assert.equal(wordCountObserverCount, 1, 'the gated observer fires when the value changes'); 35 | }); 36 | 37 | test('a changeGate without a function', function(assert) { 38 | var Hippo = EmberObject.extend({ 39 | name: 'Alex', 40 | trimmedName: computed('name', function() { 41 | return this.get('name').trim(); 42 | }), 43 | gatedTrimmedName: changeGate('trimmedName') 44 | }); 45 | 46 | var hippo = Hippo.create({ name: 'Sarah' }); 47 | assert.equal(hippo.get('gatedTrimmedName'), 'Sarah'); 48 | 49 | var observerCount = 0; 50 | var gatedObserverCount = 0; 51 | 52 | hippo.addObserver('trimmedName', function() { 53 | observerCount++; 54 | }); 55 | 56 | hippo.addObserver('gatedTrimmedName', function() { 57 | gatedObserverCount++; 58 | }); 59 | 60 | hippo.set('name', 'Sarah'); 61 | assert.equal(observerCount, 0, 'the observer does not fire when the value does not change'); 62 | assert.equal(gatedObserverCount, 0, 'the gated observer does not fire when the value does not change'); 63 | 64 | hippo.set('name', ' Sarah '); 65 | assert.equal(observerCount, 1, 'the observer does fire when the value does not change significantly'); 66 | assert.equal(gatedObserverCount, 0, 'the gated observer does not fire when the value does not change significantly'); 67 | 68 | hippo.set('name', 'Gavin'); 69 | assert.equal(observerCount, 2, 'the observer does fire when the value changes significantly'); 70 | assert.equal(gatedObserverCount, 1, 'the gated observer does not when the value changes significantly'); 71 | }); 72 | 73 | test('a changeGate without a function and multiple property dependencies', function(assert) { 74 | assert.throws(function() { 75 | EmberObject.extend({ 76 | dep1: '', 77 | dep2: '', 78 | result: changeGate('dep1', 'dep2') 79 | }); 80 | }, new Error('Assertion Failed: When depending on multiple properties a function must be passed as the last argument.')); 81 | }); 82 | 83 | test('a changeGate with multiple property dependencies', function(assert) { 84 | var Paragraph = EmberObject.extend({ 85 | text1: '', 86 | text2: '', 87 | wordCount: changeGate('text1', 'text2', function(val1, val2) { 88 | var c1 = val1.split(/\s+/).length; 89 | var c2 = val2.split(/\s+/).length; 90 | return c1 + c2; 91 | }) 92 | }); 93 | 94 | var paragraph = Paragraph.create({text1: 'hello', text2: 'world'}); 95 | assert.equal(paragraph.get('wordCount'), '2'); 96 | 97 | var text1ObserverCount = 0; 98 | var text2ObserverCount = 0; 99 | var wordCountObserverCount = 0; 100 | 101 | paragraph.addObserver('text1', function() { 102 | text1ObserverCount++; 103 | }); 104 | 105 | paragraph.addObserver('text2', function() { 106 | text2ObserverCount++; 107 | }); 108 | 109 | paragraph.addObserver('wordCount', function() { 110 | wordCountObserverCount++; 111 | }); 112 | 113 | // same count for both 114 | paragraph.set('text1', 'hi'); 115 | paragraph.set('text2', 'everyone'); 116 | assert.equal(text1ObserverCount, 1, 'the observer does fire when the value changes significantly'); 117 | assert.equal(text2ObserverCount, 1, 'the observer does fire when the value changes significantly'); 118 | assert.equal(wordCountObserverCount, 0, 'the gated observer does not fire when the value does not change'); 119 | 120 | // different count for text1 121 | paragraph.set('text1', 'hi hi'); 122 | assert.equal(text1ObserverCount, 2, 'the text1 observer does fires when the value changes significantly'); 123 | assert.equal(wordCountObserverCount, 1, 'the wordCount observer does fire when the value changes significantly'); 124 | 125 | // different count for text2 126 | paragraph.set('text2', 'hi hi'); 127 | assert.equal(text2ObserverCount, 2, 'the text2 observer does fire when the value changes significantly'); 128 | assert.equal(wordCountObserverCount, 2, 'the wordCount observer does fire when the value changes significantly'); 129 | 130 | // different count for text1 and text2 131 | paragraph.set('text1', 'a b c'); 132 | paragraph.set('text2', 'd e f'); 133 | assert.equal(text1ObserverCount, 3, 'the text1 observer does fire when the value changes significantly'); 134 | assert.equal(text2ObserverCount, 3, 'the text2 observer does fire when the value changes significantly'); 135 | assert.equal(wordCountObserverCount, 4, 'the wordCount observer does fire when the value changes significantly'); 136 | 137 | assert.equal(paragraph.get('wordCount'), 6, 'wordCount has the correct value'); 138 | }); 139 | 140 | test('a changeGate on multiple instances of same class', function(assert) { 141 | var Paragraph = EmberObject.extend({ 142 | text: 'Hello there', 143 | wordCount: changeGate('text', function(value) { 144 | return value.split(/\s+/).length; 145 | }) 146 | }); 147 | 148 | var p1 = Paragraph.create({text: 'Foo Bar baz'}); 149 | var p2 = Paragraph.create({text: 'Bar Foo'}); 150 | 151 | assert.equal(p1.get('wordCount'), 3); 152 | assert.equal(p2.get('wordCount'), 2); 153 | 154 | var p1Observer = 0; 155 | var p2Observer = 0; 156 | 157 | p1.addObserver('wordCount', function() { 158 | p1Observer++; 159 | }); 160 | 161 | p2.addObserver('wordCount', function() { 162 | p2Observer++; 163 | }); 164 | 165 | p1.set('text', 'Foo Bar Bar Boo'); 166 | assert.equal(p1Observer, 1, "the observer fires once when the value is changed on p1"); 167 | 168 | p2.set('text', "Bar Foo Foo"); 169 | assert.equal(p2Observer, 1, "the observer fires once when the value is changed on p2"); 170 | 171 | p1.set('text', 'Foo Bar Bar Bar Baa'); 172 | assert.equal(p1Observer, 2, "change to p1 is only recorded on this object, not the other"); 173 | }); 174 | 175 | test('multiple changeGate properties on same object', function(assert) { 176 | var Paragraph = EmberObject.extend({ 177 | text: 'Hello there', 178 | wordCount: changeGate('text', function(value) { 179 | return value.split(/\s+/).length; 180 | }), 181 | letterCount: changeGate('text', function(value) { 182 | return value.split('').length; 183 | }) 184 | }); 185 | 186 | var p = Paragraph.create(); 187 | 188 | var wordCountObserverCount = 0; 189 | var letterCountObserverCount = 0; 190 | 191 | p.addObserver('wordCount', function() { 192 | wordCountObserverCount++; 193 | }); 194 | 195 | p.addObserver('letterCount', function(){ 196 | letterCountObserverCount++; 197 | }); 198 | 199 | assert.equal(p.get('wordCount'), 2); 200 | assert.equal(p.get('letterCount'), 11); 201 | 202 | p.set('text', 'Hello there'); 203 | assert.equal(p.get('wordCount'), 2); 204 | assert.equal(p.get('letterCount'), 12); 205 | assert.equal(letterCountObserverCount, 1, "uneffected observer does not fire when another observer is fired"); 206 | 207 | p.set('text', 'Hello there you'); 208 | assert.equal(p.get('letterCount'), 15); 209 | assert.equal(p.get('wordCount'), 3); 210 | 211 | assert.equal(letterCountObserverCount, 2, "intended observer fires when effected"); 212 | assert.equal(wordCountObserverCount, 1, "uneffected observer does not fire when another observer is fired"); 213 | }); 214 | 215 | test("changeGate filter is bound to instance that it's attached to", function(assert) { 216 | assert.expect(1); 217 | 218 | var instance; 219 | 220 | var Paragraph = EmberObject.extend({ 221 | text: 'Hello there', 222 | wordCount: changeGate('text', function(value) { 223 | assert.equal(this, instance); 224 | return value.split(/\s+/).length; 225 | }) 226 | }); 227 | 228 | instance = Paragraph.create(); 229 | instance.get('wordCount'); 230 | }); 231 | 232 | module('setting the `sync` state', function() { 233 | test('a changeGate with a function', function(assert) { 234 | var Paragraph = EmberObject.extend({ 235 | text: 'Hello there', 236 | wordCount: changeGate('text', function(value) { 237 | return value.split(/\s+/).length; 238 | }, { sync: false }) 239 | }); 240 | 241 | var paragraph = Paragraph.create({ text: 'This is an interesting sentence' }); 242 | assert.equal(paragraph.get('wordCount'), '5'); 243 | 244 | var textObserverCount = 0; 245 | var wordCountObserverCount = 0; 246 | 247 | paragraph.addObserver('text', function() { 248 | textObserverCount++; 249 | }); 250 | 251 | paragraph.addObserver('wordCount', function() { 252 | wordCountObserverCount++; 253 | }); 254 | 255 | paragraph.set('text', 'This also has five words'); 256 | assert.equal(textObserverCount, 1); 257 | assert.equal(wordCountObserverCount, 0, 'the gated observer does not fire when the value does not change'); 258 | 259 | paragraph.set('text', 'This has four words'); 260 | assert.equal(textObserverCount, 2); 261 | assert.equal(wordCountObserverCount, 1, 'the gated observer fires when the value changes'); 262 | }); 263 | 264 | test('a changeGate without a function', function(assert) { 265 | var Hippo = EmberObject.extend({ 266 | name: 'Alex', 267 | trimmedName: computed('name', function() { 268 | return this.get('name').trim(); 269 | }), 270 | gatedTrimmedName: changeGate('trimmedName', { sync: false }) 271 | }); 272 | 273 | var hippo = Hippo.create({ name: 'Sarah' }); 274 | assert.equal(hippo.get('gatedTrimmedName'), 'Sarah'); 275 | 276 | var observerCount = 0; 277 | var gatedObserverCount = 0; 278 | 279 | hippo.addObserver('trimmedName', function() { 280 | observerCount++; 281 | }); 282 | 283 | hippo.addObserver('gatedTrimmedName', function() { 284 | gatedObserverCount++; 285 | }); 286 | 287 | hippo.set('name', 'Sarah'); 288 | assert.equal(observerCount, 0, 'the observer does not fire when the value does not change'); 289 | assert.equal(gatedObserverCount, 0, 'the gated observer does not fire when the value does not change'); 290 | 291 | hippo.set('name', ' Sarah '); 292 | assert.equal(observerCount, 1, 'the observer does fire when the value does not change significantly'); 293 | assert.equal(gatedObserverCount, 0, 'the gated observer does not fire when the value does not change significantly'); 294 | 295 | hippo.set('name', 'Gavin'); 296 | assert.equal(observerCount, 2, 'the observer does fire when the value changes significantly'); 297 | assert.equal(gatedObserverCount, 1, 'the gated observer does not when the value changes significantly'); 298 | }); 299 | 300 | test('a changeGate without a function and multiple property dependencies', function(assert) { 301 | assert.throws(function() { 302 | EmberObject.extend({ 303 | dep1: '', 304 | dep2: '', 305 | result: changeGate('dep1', 'dep2', { sync: false }) 306 | }); 307 | }, new Error('Assertion Failed: When depending on multiple properties a function must be passed as the last argument.')); 308 | }); 309 | 310 | test('a changeGate with multiple property dependencies', function(assert) { 311 | var Paragraph = EmberObject.extend({ 312 | text1: '', 313 | text2: '', 314 | wordCount: changeGate('text1', 'text2', function(val1, val2) { 315 | var c1 = val1.split(/\s+/).length; 316 | var c2 = val2.split(/\s+/).length; 317 | return c1 + c2; 318 | }, { sync: false }) 319 | }); 320 | 321 | var paragraph = Paragraph.create({text1: 'hello', text2: 'world'}); 322 | assert.equal(paragraph.get('wordCount'), '2'); 323 | 324 | var text1ObserverCount = 0; 325 | var text2ObserverCount = 0; 326 | var wordCountObserverCount = 0; 327 | 328 | paragraph.addObserver('text1', function() { 329 | text1ObserverCount++; 330 | }); 331 | 332 | paragraph.addObserver('text2', function() { 333 | text2ObserverCount++; 334 | }); 335 | 336 | paragraph.addObserver('wordCount', function() { 337 | wordCountObserverCount++; 338 | }); 339 | 340 | // same count for both 341 | paragraph.set('text1', 'hi'); 342 | paragraph.set('text2', 'everyone'); 343 | assert.equal(text1ObserverCount, 1, 'the observer does fire when the value changes significantly'); 344 | assert.equal(text2ObserverCount, 1, 'the observer does fire when the value changes significantly'); 345 | assert.equal(wordCountObserverCount, 0, 'the gated observer does not fire when the value does not change'); 346 | }); 347 | 348 | }); 349 | }); 350 | --------------------------------------------------------------------------------