Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:26:40+00:00 2026-06-10T16:26:40+00:00

I want to rewrite this code to Backbone.js, how should I do that? app/assets/javascripts/views/plots/plots_index.js.coffee

  • 0

I want to rewrite this code to Backbone.js, how should I do that?

app/assets/javascripts/views/plots/plots_index.js.coffee

class App.Views.PlotsIndex extends Backbone.View
  template: JST['plots/index']

  events:
    'submit #new_plot': 'createPlot'

  initialize: ->
    @collection.on('reset', @render, this)
    @collection.on('add', @appendPlot, this)

  render: =>
    $(@el).html(@template())
    @collection.each(@appendPlot)
    this

  appendPlot: (plot) =>
    view = new App.Views.Plot(model: plot)
    @$('#all_plots').append(view.render().el)

  createPlot: (event) ->
    event.preventDefault()
    attributes = name: $('#new_plot_name').val()
    @collection.create attributes,
      wait: true
      success: ->  $('#new_plot')[0].reset()
      error: @handleError

app/assets/templates/plots/index.jst.eco

<textarea class="input" id="new_plot_name" name="name" rows="5"  onClick="if(this.value == 'Type something') { this.value = ''; }">Type something</textarea> 
<input class="generate_button col2" name="commit" type="submit" value="Submit" />

I want to put the function from onClick into the view code, but can’t quite figure it. I tried things like this, but no luck:

    events:
        'click #new_plot_name' : 'clear'

    clear: =>
    if @$('#new_plot_name').value == 'Type something'
        @$('#new_plot_name').value = ''

What would be the way to do it, so I can do something like:

 <textarea class="input" id="new_plot_name" name="name" rows="5"  onClick="<%= @clear(this) %>">Type something</textarea>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:26:42+00:00Added an answer on June 10, 2026 at 4:26 pm

    I’m pretty sure the problem is in your clear method.

    clear: =>
      if @$('#new_plot_name').value == 'Type something'
        @$('#new_plot_name').value = ''
    

    When you say x = @$('#new_plot_name'), you get a jQuery object in x. jQuery objects generally don’t have value properties the way DOM objects do; if you want to work with the value of a form element that’s wrapped in a jQuery object, you want to use the val method:

    clear: =>
      if @$('#new_plot_name').val() == 'Type something'
        @$('#new_plot_name').val('')
    

    Then drop the onClick attribute from your template:

    <textarea class="input" id="new_plot_name" name="name" rows="5">Type something</textarea>
    

    CoffeeScript (@clear(this)) won’t work there, neither @ nor this would be what you want in that context, and clear doesn’t take an object argument anyway. Besides, this is Backbone so the events should be hooked up through the view’s events.

    Demo: http://jsfiddle.net/ambiguous/gfK4L/


    That said, people do use Tab to move around inside forms so you probably want to use a focus event (not click) to remove your placeholder and a blur event to put it back.

    You should also be using a placeholder attribute for this sort of thing; if you need to support non-HTML5 browsers then there are lots of shims and plugins that will work better than your clear method. Placeholder behavior is surprising tricky to get right, for example you’ll probably be submitting a lot of forms with name coming through as 'Type something' because you’re not checking that they really did type something in your submit handler.

    Also, there’s no need for $(@el), Backbone already supplies a jQuery wrapped @el in @$el. And in your initialize:

    initialize: ->
      @collection.on('reset', @render, this)
      @collection.on('add', @appendPlot, this)
    

    you don’t need to supply the context arguments to on since render and appendPlot are already bound methods, just this should do:

    initialize: ->
      @collection.on('reset', @render)
      @collection.on('add', @appendPlot)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to rewrite http://website.com/?slug=product_info.php&products_id=32 to http://website.com/product_info/32 and I use this code, add_filter('rewrite_rules_array','wp_insertMyRewriteRules'); add_filter('query_vars','wp_insertMyRewriteQueryVars');
I want to have URLs like this: some-company-name-deal-id-6.htm So the rewrite rule should ignore
I want to rewrite this code without so many else's, but still keep it
I am trying to rewrite this messy code, so that I only make one
I want to rewrite the code below to generate a string. This string I
ok I have this code, that I'm studying class scope{ function printme(){ return hello;
I have this URL example.com/photo?people I want to rewrite this URL in the form
I want to do Url rewrite in Tomcat using UrlRewriteFilter This is the rule
I want to rewrite core/adminthtml/block/widget/tabs.php class which is a super class for core/adminhtml/block/sales/order/view/tabs.php class.
So, I am trying to make: sub.example.com/page rewrite to www.example.com/sub/page I have this code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.