2012-07-28 9 views
7

best_in_placeの値とプレースホルダ(data-nil = "あなたの番号を入力する")を区別する方法がないため、何かが足りないと思うでしょう。私はちょうど実際の値とは違ってプレースホルダのスタイルを変えて混乱しないようにしたいと思います。スタイルのプレースホルダとbest_in_placeの値が異なる

私はイベントを調べて実際にそのクラスにクラスを追加することができましたが、初めてページに表示されたときにそのクラスを追加するイベントはありません。この時点で、私は各$( 'best_in_place')を繰り返し、spanのhtmlの値とnilの値を比較します。ただし、これを行う前に、私が何かを逃したかどうか疑問に思っています。

値がbest_in_placeで設定されていることを区別する方法はありますか?

コードがわかりやすい場合:これは、クラスをbest_in_placeに追加または削除するだけですが、ユーザーがインサイトページの読み込みではなくフィールドを編集した場合にのみ設定されます。

$('.best_in_place').bind('best_in_place:update', function(evt){ 
    if($(this).data('nil') == $(this).html()){ 
     $(this).removeClass('has_value'); 
    }else{ 
     $(this).addClass('has_value'); 
    } 
    }); 

答えて

6

ありがとうございます。私は

$('.top-column .best_in_place').each(function() { 
    var element = $(this); 

    if(element.data('nil') != element.text()) { 
    updateBestInPlace($(this)); 
    } 
}); 

私example.js.erbで

でこれを解決され、example.jsに

$(document).ready(function(){ 
    $('.top-column .best_in_place').live("ajax:success", function(){ 
    updateBestInPlace($(this)); 
    }); 
}); 

function updateBestInPlace(element){ 
    if(element.data('nil') == element.html() || element.html() == ""){ 
    element.removeClass('has_value'); 
    }else{ 
    element.addClass('has_value'); 
    } 
} 
5

1つの非常に簡単な解決策は、このように、あなたの:nil値でマークアップを含めることです。

= best_in_place @user, :email, nil: '<span class="none-yet">None yet!</span>' 

次に、次のようなCSSルールを設定できます。

.none-yet { 
    font-style: italic; 
} 

これはgitのからリビジョンdf178520bf9ba405baee9528d5dbb630d6ff760cとの私のために働いている://github.com/bernat/best_in_place.git

+0

天才!ありがとう:) –

関連する問題