jQuery REST
Add shortcuts to make PUT
and DELETE
AJAX requests:
$.put('/posts/1', { title: 'New' }, function(message) {
alert('Saved. ' + message)
})
$.del('/posts/1', function(message) {
alert('Deleted. ' + message)
})
Because some browsers and firewalls have problems with PUT
and DELETE
requests, shortcuts create POST
request with X-HTTP-Method-Override
header
with correct method name.
All Rack frameworks (Ruby on Rails, Sinatra) support this override.
For plain Rack app you need to use Rack::MethodOverride
middleware.
Shorcuts have same arguments as $.post
(url, data, callback, dataType),
so you can set response type on last argument.
They just shortcuts under $.ajax
, so they return $.Deferred
object:
$.delete('/posts/1').
success(function() { alert('Deleted') }).
error(function() { alert('Error') })
Install
Rails 3.1
In Rails 3.1 (or another project with Sprockets 2) just add jquery-rest
to
Gemfile
:
gem 'jquery-rest'
and add require to app/assets/javascripts/application.js.coffee
:
#= require "jquery.rest"
Jammit
If you use Jammit or another package manager just copy lib/jquery.rest.js
to
public/javascripts/lib
in your project and add library to config/assets.yml
:
javascripts:
application:
- public/javascripts/lib/jquery.rest.js
Other
If you didn’t use Rails 3.1 or assets packaging manager you can use already
minimized version of library at lib/jquery.rest.min.js
.
Contributing
To run project tests and minimize source you must have Ruby and Bundler. For example, on Ubuntu:
sudo apt-get install ruby rubygems
sudo gem install bundler
Next you need install Jasmine, UglifyJS and other dependencies by Bundler. Run in project root:
bundle install --path=.bundle
That’s all. To run tests start server and open http://localhost:8888/:
bundle exec rake server
Before commit minimize project source:
bundle exec rake min