Skip to content

Commit

Permalink
working update in backbone and refactored controller
Browse files Browse the repository at this point in the history
  • Loading branch information
kerrieyee committed Feb 5, 2013
1 parent 8deacb8 commit fe6b2b7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
23 changes: 23 additions & 0 deletions app/assets/javascripts/views/job_prospects/job_prospect.js.coffee
Expand Up @@ -9,6 +9,7 @@ class JobOrg.Views.JobProspect extends Backbone.View

initialize: ->
@model.on('destroy', @unrenderJobProspect, this)
@model.on('save', @rerenderJobProspect, this)

render: ->
$(@el).html(@template(job_prospect: @model))
Expand All @@ -18,6 +19,28 @@ class JobOrg.Views.JobProspect extends Backbone.View
event.preventDefault
@model.destroy()

editJobProspect: (event, job_prospect) ->
event.preventDefault()
$('form#new_job_prospect').hide()
$('form#edit_job_prospect').show()
$('#edit_job_prospect_company').val(@model.get('company'))
$('#edit_job_prospect_position').val(@model.get('position'))
$('#edit_id').val(@model.get('id'))

handleError: (job_prospect, response) ->
if response.status == 422
errors = $.parseJSON(response.responseText).errors
for attribute, messages of errors
for message in messages
$('.alert-error').text("#{attribute} #{message}.")
$('.alert-error').show()
$('.alert-success').hide()
$('.alert-warning').hide()
$('.alert-notice').hide()

rerenderJobProspect: ->
$(@el).html(@template(job_prospect: @model))
this

unrenderJobProspect: ->
$(@el).remove()
Expand Down
Expand Up @@ -4,6 +4,8 @@ class JobOrg.Views.JobProspectsIndex extends Backbone.View

events:
'submit #new_job_prospect': 'createJobProspect'
'click input[value="Update Job Prospect"]': 'updateJobProspect'


initialize: ->
@collection.on('reset', @render, this)
Expand Down Expand Up @@ -33,6 +35,27 @@ class JobOrg.Views.JobProspectsIndex extends Backbone.View
$('.alert-warning').hide()
$('.alert-notice').hide()
error: @handleError

updateJobProspect: (event, job_prospect) ->
event.preventDefault()
attributes =
company: $('#edit_job_prospect_company').val()
position: $('#edit_job_prospect_position').val()
editJob = @collection.get($('#edit_id').val())
editJob.set attributes
editJob.save attributes,
wait: true
success: ->
$('#edit_job_prospect')[0].reset()
$('#edit_job_prospect').hide()
$('#new_job_prospect').show()
$('.alert-success').text "Your job prospect has been successfully updated."
$('.alert-error').hide()
$('.alert-success').show()
$('.alert-warning').hide()
$('.alert-notice').hide()
editJob.trigger("save")
error: @handleError

handleError: (job_prospect, response) ->
if response.status == 422
Expand Down
3 changes: 3 additions & 0 deletions app/assets/templates/job_prospects/index.jst.eco
Expand Up @@ -11,13 +11,16 @@
<input type="text" name="company" id="edit_job_prospect_company"><br/>
Position:</br>
<input type="text" name="position" id="edit_job_prospect_position"><br/>
<input type="hidden" name="id" id="edit_id">
<input type="submit" value="Update Job Prospect" class="btn btn-small btn-info">
</form>
<table class="table">
<thead>
<tr>
<th> Company </th>
<th> Position </th>
<th> Edit </th>
<th> Delete </th>
</tr>
</thead>
<tbody id="jobprospects">
Expand Down
24 changes: 5 additions & 19 deletions app/controllers/job_prospects_controller.rb
@@ -1,38 +1,24 @@
class JobProspectsController < ApplicationController
respond_to :json, :html
def index
@job_prospects = JobProspect.paginate(:page => params[:page],
:per_page => 8,
:order => 'created_at DESC',
:conditions => { :user_id => current_user.id })
@job_prospects = JobProspect.where(:user_id => current_user.id)
respond_with @job_prospects
end

# def new
# @job_prospect=JobProspect.new
# respond_to do |format|
# format.html { render :action => "new" }
# format.js
# end
# end


def create
@job_prospect = JobProspect.create( :company => params[:job_prospect][:company],
:position => params[:job_prospect][:position],
:user => current_user)
respond_with @job_prospect
end

# def edit
# @job_prospect = JobProspect.find(params[:id])
# respond_to { |format| format.js }
# end

def update
@job_prospect = JobProspect.find(params[:id])
@job_prospect.update_attributes(:company => params[:job_prospect][:company],
:position => params[:job_prospect][:position],
:user => current_user)

@job_prospect.update_attributes(params[:job_prospect]) ? respond_with(@job_prospect) : respond_with(@job_prospect, :status => unprocessable_entity)
respond_with(@job_prospect)
end

def show
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -2,11 +2,12 @@

devise_for :users

root :to => "main#index"

scope "api" do
resources :job_prospects
end

root :to => "main#index"
resources :job_prospects do
resources :events
end
Expand Down

0 comments on commit fe6b2b7

Please sign in to comment.