From 5cd98475cc3a7e06fb5c95b7238bf432a9381b8d Mon Sep 17 00:00:00 2001 From: Zoker Date: Sat, 15 Oct 2016 15:57:11 +0800 Subject: [PATCH] task over function finished --- app/controllers/tasks_controller.rb | 13 +++------- app/views/plans/show.html.haml | 37 ++++++++++++++++++++++++++-- app/views/plans/show.json.jbuilder | 1 + app/views/tasks/create.json.jbuilder | 3 ++- app/views/tasks/update.json.jbuilder | 3 +++ 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 app/views/tasks/update.json.jbuilder diff --git a/app/controllers/tasks_controller.rb b/app/controllers/tasks_controller.rb index 0b97b5c..f32aca4 100644 --- a/app/controllers/tasks_controller.rb +++ b/app/controllers/tasks_controller.rb @@ -1,6 +1,6 @@ class TasksController < ApplicationController before_action :authenticate_user! - before_action :set_plan_list, only: [:new, :create] + before_action :set_plan_list, only: [:new, :create, :update] before_action :set_task, only: [:update, :action, :destroy] # GET /tasks/new @@ -34,17 +34,10 @@ class TasksController < ApplicationController end end - # PATCH/PUT /tasks/1 # PATCH/PUT /tasks/1.json def update - respond_to do |format| - if @task.update(task_params) - format.html { redirect_to @task, notice: '更新任务成功!' } - format.json { render :show, status: :ok, location: @task } - else - format.html { render :edit } - format.json { render json: @task.errors, status: :unprocessable_entity } - end + unless @task.update(task_params) + render json: {status: 'failed', message: '任务更新失败, 请稍后重试 !'} end end diff --git a/app/views/plans/show.html.haml b/app/views/plans/show.html.haml index 14c06a0..3d9696a 100644 --- a/app/views/plans/show.html.haml +++ b/app/views/plans/show.html.haml @@ -48,20 +48,23 @@ .modal-footer %button.btn.btn-default{'data-dismiss' => 'modal', :type => 'button'} 取消 %button.btn.btn-danger{:type => 'button', 'v-on:click' => 'deleteList'} 确认删除 + .panel-body .add-task-field = render 'tasks/form' %p{'v-if' => 'list.tasks.length == 0'} 暂无相关任务 + %template{'v-for' => 'task in list.tasks'} .task-field %span.task-item{':class' => 'task.status_class'} {{task.title}} .task-operations - .non-group + = hidden_field_tag :task_path, nil, 'v-model' => 'task.path' + .non-group{'v-show' => 'task.status == 1'} %i.icon-pencil .non-group %i.icon-remove - %button.btn.btn-primary.btn-xs + %button.btn.btn-primary.btn-xs{'v-on:click' => 'updateTask', 'v-show' => 'task.status == 1'} over .col-md-3#new_list_area{ 'v-show' => 'newListField' } #new_list_field @@ -90,6 +93,7 @@ color_tag_class: '', path:'', tasks: [{ + path: '', title: '', status: '', status_class: '' @@ -190,6 +194,35 @@ xhr.send() $(event.currentTarget).closest('.delete_list_modal').modal('hide') }, + updateTask: function() { + var xhr = new XMLHttpRequest() + var data = this.plan.lists + var listPath = $(event.currentTarget).closest('.panel').find('#new_task_path').val() + var path = $(event.currentTarget).parent().find('#task_path').val() + var list = false + var task = false + for (var i = 0; i < data.length; i++){ + if ( data[i].path == listPath ){ + list = data[i] + } + } + xhr.open("PUT", path + '.json') + xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded") + xhr.onload = function () { + result = JSON.parse(xhr.responseText) + if (result.status == 'failed') { + notifyTop(result.message, 'danger') + } else { + for (var i = 0; i < list.tasks.length; i++){ + if ( list.tasks[i].path == path ){ + list.tasks.splice(i, 1, result) + } + } + notifyTop('任务更新成功 !') + } + } + xhr.send('task[status]=0') + }, addList: function () { var xhr = new XMLHttpRequest() var self = this diff --git a/app/views/plans/show.json.jbuilder b/app/views/plans/show.json.jbuilder index 147fcff..5a811c9 100644 --- a/app/views/plans/show.json.jbuilder +++ b/app/views/plans/show.json.jbuilder @@ -7,5 +7,6 @@ json.lists @lists do |list| json.tasks list.tasks do |task| json.(task, :title, :status) json.status_class task.status == 0 ? 'closed-task' : '' + json.path plan_list_task_path(@plan, list, task) end end \ No newline at end of file diff --git a/app/views/tasks/create.json.jbuilder b/app/views/tasks/create.json.jbuilder index 339f549..88d462c 100644 --- a/app/views/tasks/create.json.jbuilder +++ b/app/views/tasks/create.json.jbuilder @@ -1,2 +1,3 @@ json.extract! @task, :title, :status -json.status_class '' \ No newline at end of file +json.status_class '' +json.path plan_list_task_path(@plan, @list, @task) \ No newline at end of file diff --git a/app/views/tasks/update.json.jbuilder b/app/views/tasks/update.json.jbuilder new file mode 100644 index 0000000..a40e6c5 --- /dev/null +++ b/app/views/tasks/update.json.jbuilder @@ -0,0 +1,3 @@ +json.extract! @task, :title, :status +json.status_class @task.status == 0 ? 'closed-task' : '' +json.path plan_list_task_path(@plan, @list, @task) \ No newline at end of file