serializeData vs. templateHelpers?

社会主义新天地 提交于 2020-01-04 13:39:15

问题


In Marionette, afaik these two snippets achieve the same purpose:

serializeData: function() {
  data = super;

  data.foo = "bar";

  return data;
}

and

templateHelpers: function() {
  return {
    foo: "bar"
  }
}

What would be the difference and when to use one or the other?


回答1:


In your example I can't think of any practical difference.

Semantically, though, I think that serializeData() is the better fit when you're transforming the existing model data into something else. It's more complicated to use, because you have to think about the default serialization of the model, which could include a custom model.toJSON() call. For example, your model might override toJSON to avoid sending some unneeded data to the server, but if you need that data in the view you will want to add it back with serializaData().

templateHelpers, on the other hand, is more straightforward, since all it does is add new attributes. It makes sense when there's some computed data you want to have available in the template that isn't part of your model.

In the example you cite above, I would use templateHelpers, because it's a better fit semantically and because it's simpler.



来源:https://stackoverflow.com/questions/32224502/serializedata-vs-templatehelpers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!