banner 728x90

Watch out for object changes with JavaScript

In Mobile Web Development 84 views
banner 728x90

Looking at changes to the property of an object has always been a much requested task; Over the years, many discs have been used to listen to object changes. Nowadays we have better methods to listen to object changes: the proxy API. Sindre Sorhos has made on-change, a small JavaScript tool that lets you play for changes on JavaScript objects and array & # 39; s!

Change JavaScript

Because the replacement code is so small, here it is in its full glory:

& # 39; Strictly use & # 39;

module.exports = (object, onChange) => ;

The onChange function returns a proxy that you can use to modify an object.

Using on-change

Indicate at Change the object you want to spy on and a function for changing handlers:

let j = ;

// Save to server when saving
let changeable = onChange (j, () => save (j));

Then use that proxy to change properties and receive notifications of changes:


// Make a change that would cause changes
changeable.a = 2;

// save () is triggered!

// j.a === 2

Note that the values ​​of the original object also change – that's the beauty of Proxy! Also note that when changing is not indicated which property is changed; the use case, as Sindre describes, is to store an object (to server, etc.) when a part of an object changes. You could also use ti for a small library with a rendering function, where the content is redisplayed when a property is changed!

This exchange library is really nice if you do not have to know which house has changed, only that something has changed.


banner 728x90
author
Author: 
    Build a user settings store with AWS AppSync
    Build a user settings store with AWS AppSync
    Amazon Cognito Sync is a service that
    Synchronize user settings for Android with AWS AppSync
    Synchronize user settings for Android with AWS AppSync
    Amazon Cognito Sync is a service that
    Script & Style Show: Episode 10: Dojo
    Script & Style Show: Episode 10: Dojo
    In this episode welcome David and Todd

    Leave a reply "Watch out for object changes with JavaScript"

    Must read×

    Top