JSONP Support #25

Closed
mdespuits opened this Issue Feb 21, 2012 · 6 comments

Comments

Projects
None yet
2 participants
@mdespuits

As this has some bearing with Javascript web applications, any plans on adding support for JSONP output?

@bigjason

This comment has been minimized.

Show comment
Hide comment
@bigjason

bigjason Apr 4, 2012

Contributor

JSONP is not really a format, it is more of a javascript technique really. The hard part being that on the server, you specify a callback for the client. Where would that callback be coming from? Would it be different in each request?

I am not trying to rain on any parades. I actually have a need for this as well, but can't quite picture the implementation. Do you have anything in mind?

Contributor

bigjason commented Apr 4, 2012

JSONP is not really a format, it is more of a javascript technique really. The hard part being that on the server, you specify a callback for the client. Where would that callback be coming from? Would it be different in each request?

I am not trying to rain on any parades. I actually have a need for this as well, but can't quite picture the implementation. Do you have anything in mind?

@mdespuits

This comment has been minimized.

Show comment
Hide comment
@mdespuits

mdespuits Apr 4, 2012

Not in particular. The only thing I noticed is that there was not a very simple way to wrap the JSON output jBuilder makes with a function callback. That's really what I was thinking about was the just the function wrapper. If this is not planned or is better fit somewhere else, I completely understand, but I really like using this gem and figured I would ask if that was a possibility.

Not in particular. The only thing I noticed is that there was not a very simple way to wrap the JSON output jBuilder makes with a function callback. That's really what I was thinking about was the just the function wrapper. If this is not planned or is better fit somewhere else, I completely understand, but I really like using this gem and figured I would ask if that was a possibility.

@bigjason

This comment has been minimized.

Show comment
Hide comment
@bigjason

bigjason Apr 4, 2012

Contributor

It may be. I am not actually affiliated with @dhh or anything. My company just uses this gem heavily and so we contribute when we can.

If you are using rails 3.1+ I think that you could actually write a sprockets processor to do this for you. Something that takes the output of the previous step and wraps it in a function. Something like:

class JSONPTemplate < Tilt::Template
  def self.default_mime_type
    'application/javascript'
  end
  def prepare
  end

  def evaluate(scope, locals, &block)
    "somethingClientSide(#{data});"
  end
end

Sprockets.register_engine '.jsonp', JSONPTemplate

Then you name your templates something like accounts.jsonp.jbuilder and it will create the jsonp output exactly how you want it.

Contributor

bigjason commented Apr 4, 2012

It may be. I am not actually affiliated with @dhh or anything. My company just uses this gem heavily and so we contribute when we can.

If you are using rails 3.1+ I think that you could actually write a sprockets processor to do this for you. Something that takes the output of the previous step and wraps it in a function. Something like:

class JSONPTemplate < Tilt::Template
  def self.default_mime_type
    'application/javascript'
  end
  def prepare
  end

  def evaluate(scope, locals, &block)
    "somethingClientSide(#{data});"
  end
end

Sprockets.register_engine '.jsonp', JSONPTemplate

Then you name your templates something like accounts.jsonp.jbuilder and it will create the jsonp output exactly how you want it.

@bigjason

This comment has been minimized.

Show comment
Hide comment
@bigjason

bigjason Apr 6, 2012

Contributor

Uhhh. Don't try to use the above. That is for assets not for templates.

Contributor

bigjason commented Apr 6, 2012

Uhhh. Don't try to use the above. That is for assets not for templates.

@bigjason

This comment has been minimized.

Show comment
Hide comment
@bigjason

bigjason Apr 6, 2012

Contributor

Ok I have made a solution. It is an alternate template extension that will look for a callback param and format to JSONP if it finds it. You can get it here: https://rubygems.org/gems/jpbuilder. I think that it may work as an inclusion in Jbuilder so I may create a pull request for it later.

Contributor

bigjason commented Apr 6, 2012

Ok I have made a solution. It is an alternate template extension that will look for a callback param and format to JSONP if it finds it. You can get it here: https://rubygems.org/gems/jpbuilder. I think that it may work as an inclusion in Jbuilder so I may create a pull request for it later.

@mdespuits

This comment has been minimized.

Show comment
Hide comment
@mdespuits

mdespuits Apr 6, 2012

Wow, that's fantastic! Thanks @bigjason.

Wow, that's fantastic! Thanks @bigjason.

@mdespuits mdespuits closed this Apr 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment