[wp-trac] [WordPress Trac] #43149: WP-API JS Client not triggering change event when using setMeta/setMetas helpers

WordPress Trac noreply at wordpress.org
Tue Jan 23 16:49:57 UTC 2018


#43149: WP-API JS Client not triggering change event when using setMeta/setMetas
helpers
----------------------------------+-----------------------------
 Reporter:  caercam               |      Owner:
     Type:  defect (bug)          |     Status:  new
 Priority:  normal                |  Milestone:  Awaiting Review
Component:  General               |    Version:  4.9
 Severity:  normal                |   Keywords:
  Focuses:  javascript, rest-api  |
----------------------------------+-----------------------------
 As briefly
 [https://wordpress.slack.com/archives/C5UNMSU4R/p1516717879000467
 mentioned] on Slack earlier today the `model.setMeta()` helper method
 introduce in #41055 has a couple of weaknesses.

 1/ The `meta` attribute is undefined when creating a new model, which
 makes sense, but causes `setMetas()` to silently fail at set new meta
 values; using `setMeta()` causes an error.

 {{{#!javascript
 var post = new wp.api.models.Post( { id : 1234 } );
 post.setMetas( { foo : 'bar' } ); // post.attributes.meta remains
 undefined
 post.setMeta( 'foo', 'bar' ); // TypeError: metas is undefined
 }}}

 2/ When the  `meta` attribute is properly defined, some changes in meta
 values fail to trigger a change event. Provided that the above issue does
 not show:

 {{{#!javascript
 var post = new wp.api.models.Post( { id : 1234 } );
 post.on( 'change:meta', console.log );
 post.set( 'meta', { foo : 'bar' } ); // trigger change event
 post.setMetas( { foo : 'BAR' } ); // Does not trigger change event
 post.setMeta( 'foo', 'BAR' ); // Does not trigger change event
 }}}

 Values are correctly updated; the `change` event is just not triggered as
 it should.

 The only way I found to make this work is to clone the `meta` attribute in
 `setMeta()`/`setMetas()`. As of now these methods simply retrieve the
 `meta` attribute using `get()` and then `_.extend()` to add/update
 properties, which doesn't seem enough to make Backbone consider it a
 change.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43149>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list