atticoos / angular-translate-once

:currency_exchange: Extension of angular-translate for one time bindings
53 stars 11 forks source link

translate-values #12

Closed w-sz closed 8 years ago

w-sz commented 8 years ago

Do you support translate-values, when is fill up after initialize with some server call ?

I don't see any $observe in code. You can add this and kill watcher after when value is there.

http://stackoverflow.com/questions/20926503/how-to-stop-observe-in-angularjs

atticoos commented 8 years ago

Yes translate-values are supported but they must be resolved as this library only binds once.

You can use an ng-if to keep the element from compiling until all the data is ready

w-sz commented 8 years ago

@ajwhite but is not better to observe attribute and when is value there - stop ? Ng-if looks like little bit hack. Right now you fill up it immediately, but translate-values="{value}" can come later.

atticoos commented 8 years ago

To the contrary, I find that observation defeats the purpose of one time binding.

  1. if there are many values that may all resolve at different times, we shouldn't be using a one time binding
  2. If there's even one value that might resolve later on in the future, whatever component using this value for a translation sounds like it shouldn't be on the screen until its dependent data has arrived.
  3. There comes a large question of "when do I stop observing?". Usually the answer is "when the value comes back", but how do we know? What if the value that comes back is null, or undefined? We have to make assumptions for areas that we know nothing about.

I feel pretty strong about enforcing single-use behavior on this directive. When situations like this come up, I feel that the implementation in how this tool is used needs to be evaluated. The question I usually ask myself is "If this translation depends on a value, and that value does not yet exist, why am I trying to present this translation?"

w-sz commented 8 years ago

I agree with your point of view, but:

  1. empty HTML element do not need to be hide.
  2. if I use ng-if, then what is the point to translate-once if element will be bind anyway.
  3. if you will add observe and pass isUndefinedOrNull then I will know that something is wrong in my data, because element is not display (empty HTML).

In my opinion is much more secure to observe element, because in this moment weak connection can make field empty.

atticoos commented 8 years ago

Let's look at this this another way.

Say you were to take one of these values that doesn't resolve until a later point in time.

You'd likely want to avoid one time binding it, as the initial empty value will never reevaluate

{{ ::valueLoadingLater }}

You would bind it regularly

{{ valueLoadingLater }}

I'd suggest you use the regular translate directive for values that should not undergo one time binding.

To your points:

  1. I'm not really interested in arguing this as I feel differently.
  2. I encourage you to read how ngIf works. Anything it contains will be removed from the DOM when false, and when it comes back when true, the directives will go through their link phase.
  3. This means the library is making assumptions that all things must be this way. As a library, it should expect that some scenarios a value very well might come back as undefined or null. We cannot know this for sure, as we are only a library.

I'm sorry that this does not support observation, as this is meant to only bind once. I'd suggest a few things for you moving forward:

w-sz commented 8 years ago

@ajwhite ng-if will set-up watcher on element, then what is the point to translate it once?

You are just ignorant - no more and because of that you will never by good.