Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere do I need a closure for this one?
    primarykey
    data
    text
    <p>I have this constructor:</p> <pre><code>var EditableItem = (function() { function EditableItem(schema, item) { this.schema = _.clone(schema); this.item = _.clone(item); this.propertyTypes = [ 'stringProperties', 'booleanProperties']; } EditableItem.prototype = { createItem: function() { var self = this; self.propertyTypes.forEach(function(type) { var properties = self.schema[type]; properties.forEach(function(property, i) { var itemProperty = self.item[type][property.id]; if (itemProperty) { properties[i].value = itemProperty; } }); }); self.schema.itemId = self.item._id; return self.schema; } }; return EditableItem; })(); </code></pre> <p>and each time I use it, like this...</p> <pre><code>async.parallel({ schema: function(callback) { schemas().findOne({ _id: 'default' }, callback); }, items: function(callback) { items().find().toArray(callback); } }, function(err, result) { if (err) return callback(err); var all = result.items.map(function(item) { return new EditableItem(result.schema, item).createItem(); }); callback(null, all); }); </code></pre> <p>...I end up with an array <code>result</code> where the last item is repeated while the others are omitted.</p> <p>My guess is that I need to add a closure somewhere, and as you can see I've tried, but still it yields the same result. Any ideas?</p> <p><strong>EDIT:</strong> I've found a solution. It's not pretty, but maybe someone here can explain why it works and offer a better solution...</p> <p>In the constructor, where I had: this.schema = _.clone(schema);</p> <p>I changed to: this.schema = JSON.parse(JSON.stringify(schema));</p> <p>This seems to allocate new memory for the object, while _.clone still keeps some references to the original object (I guess).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload