今天,首先貼上updateBook的ajax
先 npm install rx-dom;
//in clients/app/bundles/containers/ReadingList.jsx
allBooks(bookAttributes) {
let settings = { url: Routes.books_path(), responseType: 'json'}
RxDOM.ajax(settings)
.subscribe(
(data) => {
let books = data.response.books;
this.setState({books});
},
function (error) {
}
);
}
//in clients/app/bundles/containers/ReadingList.jsx
deleteBook(id) {
let settings = { url: Routes.book_path(id), responseType: 'json', method: 'DELETE'}
RxDOM.ajax(settings)
.subscribe(
(data) => {
},
(error) => {
}
);
}
updateBook(bookAttributes, id) {
let settings = id == 0? { url: Routes.books_path(), responseType: 'json', method: 'POST', body: bookAttributes} :
{ url: Routes.book_path(id), responseType: 'json', method: 'PUT', body: bookAttributes}
RxDOM.ajax(settings)
.subscribe(
(data) => {
let books = data.response.books;
console.log(books);
this.setState({books});
},
function (error) {
}
);
}
updateBookStatus(id, status) {
let settings = {
url: Routes.status_book_path(id),
responseType: 'json',
method: 'POST',
headers: {
'X-Requested-With': 'RxJS',
'Content-Type': 'application/json;charset=utf-8'
},
body: JSON.stringify({book:{status: status}})
}
RxDOM.ajax(settings)
.subscribe(
(data) => {
let books = data.response.books;
this.setState({books});
},
function (error) {
}
);
}
然後 修改他的render
//in clients/app/bundles/containers/ReadingList.jsx
render() {
return (
<div>
<ReadingListWidget books={this.state.books}
updateBook={(attributes) => this.updateBook(attributes)}
updateBookStatus={(id, status) => this.updateBookStatus(id, status)}
deleteBook={(id) => this.deleteBook(id)}/>
</div>
);
}
def update_status
status = params[:status]
respond_to do |format|
if @book.update(book_params)
@books=Book.includes(:author)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { render action: :index, status: :ok, location: @book }
else
format.html { render :edit }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
帥慘~終於結合起來了!!當然還有children的props沒打,
詳細請看https://github.com/josephMG/itHelpRRRR!!
打完可以洗洗睡了!!