Answer the question
In order to leave comments, you need to log in
Javascript(Coffee) best practice: how can I do it better?
I ask the respected community to offer me a technique that resolves the question: “How can I make a hoiku out of this hexameter?”
$('.dropdown-toggle').parent().find('.dropdown-element').click ->
$(this).parents('.btn-group').find('.dropdown-toggle').text(this.textContent)
if $('.container').data($(this).data('attribute')) == undefined
console.log 'undef'
$('.container').data($(this).data('attribute'), new Object)
if $(this).data('place')
if $('.container').data($(this).data('attribute'))[$(this).data('place')] == undefined
$('.container').data($(this).data('attribute'))[$(this).data('place')] = new Object
$('.container').data($(this).data('attribute'))[$(this).data('place')][$(this).data('change')] = $(this).data($(this).data('change'))
else
$('.container').data($(this).data('attribute'))[$(this).data('change')] = $(this).data($(this).data('change'))
wheels: Object
front_left: Object
brand: "Barum"
diameter: "14"
profile: "10.5"
protector: "4"
width: "32"
__proto__: Object
front_right: Object
brand: "Continental"
profile: "12.5"
protector: "6"
width: "155"
Started PUT "/reports/9" for 127.0.0.1 at 2012-07-24 17:51:34 +0600
Processing by ReportsController#update as */*
Parameters: {
"report"=>{
"wheels"=>{
"rear_left"=>{
"width"=>"135",
"profile"=>"10.5",
"diameter"=>"14",
"protector"=>"3",
"brand"=>"Barum"
},
"front_left"=>{
"brand"=>"Barum",
"width"=>"32",
"profile"=>"10.5",
"diameter"=>"14",
"protector"=>"4"
}
},
"id" => 9}
}
@report = Report.find(params[:id])
@report.update_attributes(params[:report])
HAML
%ul.dropdown-menu.pull-right
- (12..24).to_a.each do |i|
%li
.dropdown-element{:data => {:attribute => 'wheels',:place => 'front_right', :change => 'diameter', :diameter => i}}=i
Answer the question
In order to leave comments, you need to log in
The first piece of code is an amazing example of why jQuery chaining is evil.
It's a little difficult to understand all this without a specific example, so I'll just suggest tweaking the code a bit.
$('.dropdown-toggle').parent().find('.dropdown-element').click ->
$(@).parents('.btn-group').find('.dropdown-toggle').text(@textContent)
attr = $(@).data('attribute')
console.log('undef') unless $('.container').data(attr)
container_data = $('.container').data(attr) || new Object
change = $(@).data('change')
place = $(@).data('place')
if place
container_data[place] ||= new Object
container_data[place][change] = $(@).data(change)
else
container_data[change] = $(@).data(change)
$('.container').data(attr,container_data)
Use internal variables and don't spawn a sea of strings like...
$(this).data('place')
var place = $(this).data('place')
Can you clarify?
1. there is css
.pull-right {float: right}
why is span.pull-right and div.pull-right bad?
2. When I say green, I can mean the button and the background.
But when I write the button. green, I know, and I will not be mistaken that I work with the button, I can accidentally write. brown, but I don’t have a brown button, but .brown will be found in the house. Dealing with an unexpected object can lead to unwanted batharts.
A specialist who will, God forbid, work with my code will be better oriented in the tree if button.dropdown-toggle is written.
It brings order.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question