1

I have a model with two arrays, bright and normal. Each array includes objects which are representing colors. I want to implement in model a method allowing to set some properties (for example hue) of this colors. So I wrote this:

setHue: function(hue) {
    _.each([this.get('brigth'), this.get('normal')], function(colors) {
        _.each(colors, function(color) {
            color.setHue(hue + this.getHue());
        });
    });
},

I think it's clear - I try to iterate over every color in both bright and normal arrays. And it doesn't update colors inside these arrays. This color inside _.each seems to have a new value, but it looks it's only a copy of color, not a reference. Do anyone know how to do what I want? I mean with _.each loop, I don't wanna mess up with for, length and indexes.

4

2 に答える 2

1

ブライトのスペルが間違っています。あなたは明るく入りました。

于 2012-08-18T01:59:34.610 に答える
0

うーん、ここで一般的な問題があると思います。例を見てみましょう:

this.get('brigth')

モデルではなく配列を返しますよね?

もしそうなら、

_.each([this.get('brigth'), this.get('normal')], function(colors) { ...

配列の配列をループします。つまり、色はモデルやコレクションではなく、配列を取得します。したがって、色は配列の項目になります (文字列を想定しています)。したがって、色には setHue メソッドがありません。

さらに、Paul も正しい this.getHue() これは、コンテキストを 2 回 (各ループごとに 1 回) 切り替えるため、モデルを参照しません。ここで完全に間違っていなかったことを願っています。

于 2012-08-17T08:11:42.553 に答える