Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript arrays and Meteor session
    primarykey
    data
    text
    <p>I have made an interesting observation. When trying to update an array that is stored in the Meteor session storage, the following code will not propagate the changes:</p> <pre><code>var tags = Session.get("Tags"); tags.push("a"); Session.set("Tags", tags); </code></pre> <p>But if I change the first line to use <code>Session.get("Tags").slice()</code>, everything depending on the session will update accordingly. I guess this is due to the fact that Meteor tests some references for equality and therefore does not update anything.</p> <p><strong>Is there a better way to manage lists stored in the meteor session store?</strong></p> <p>If I now try to remove an element from the collection (using <code>array.remove()</code> from <a href="http://ejohn.org/blog/javascript-array-remove/" rel="noreferrer">here</a>), the behavior turns out to be a bit ... of ... I am doing this inside a Meteor template event, the code looks like this:</p> <pre><code>"click .taglist li" : function(e) { var tags = Session.get("Tags").slice(); var index = cardTags.indexOf(this); Meteor._debug(Session.get("Tags").slice().indexOf("a")); Meteor._debug("Removing tag \"" + this + "\", index: " + index, ", typeof(this) = " + typeof(this).toString()); tags.remove(index); Session.set("Tags", tags); } </code></pre> <p>This outputs:</p> <pre><code>1 Removing tag "a", index: -1, typeof(this) = string </code></pre> <p>So somehow, the <code>cardTags.indexOf(this);</code> statement seems to return <code>-1</code> for almost any case. I guess I am doing something fundamentally wrong, as I am quite now to javascript, but somehow I can not figure out whats going on here.</p> <p><strong>Why will those two calls to indexOf() behave different?</strong></p>
    singulars
    1. This table or related slice is empty.
    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