javascript updateRow question

  • 1
  • Question
  • Updated 3 years ago
  • Answered
Can I replace this

model.updateRow(row, {UpVotes: row.UpVotes + 1});

with this

row.UpVotes++

??

Does that accomplish the same thing?
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb

Posted 3 years ago

  • 1
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
No, they do not do the same thing. Your first way is the correct way. The second way, row.UpVotes++ introduces a "side effect". It is actually setting the property row.UpVotes as one greater than the current value of row.UpVotes. You don't want to do this because this would mean you're going around the Skuid API, and directly modifying a property on a model. Then, after that, you are using the Skuid API, which is kind of silly.
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
To be clear, my comment was contrasting these two options.

model.updateRow(row, {UpVotes: row.UpVotes + 1});

and

model.updateRow(row, {UpVotes: row.UpVotes++ });
Photo of Ben Hubbard

Ben Hubbard, Employee

  • 12,490 Points 10k badge 2x thumb
If you're contrasting

model.updateRow(row, {UpVotes: row.UpVotes + 1});

and

row.UpVotes++

Then the answer is the same, but for different reasons. The first way is correct, and the second way is wrong. Skuid's implementation of updateRow is hundreds of lines of Javascript that take care of many, many things. Just directly modifying model properties will get you into trouble, fast. :)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
This was the version I intended to contrast. Thanks.