Hi George,

In this case it really doesn't make much difference.  There just happens to
be more than way to skin the cat.  You have a context, an instance of
ITextEdit, and you're trying to configure BFG in such a way that it can call
different views based on the request.  You've already done this using one
perfectly legitimate approach--you've given your view a name and used the
name in the url that you call to retrieve it.  It's unambiguous which view
is being called here, so there's no real need to go further.

The xhr predicate is just another way to differentiate view registrations.
Rather than using the name of the view, and adding the name to the end of
the url you use to call it, you could, instead, use the xhr predicate.
Since TinyMCE is issuing an XMLHTTPRequest in order to hit your view, and
sets a header in the request indicating that, you can use the xhr predicate
to look for the presence of that header and call the view that way.  The url
you use to hit the image list js and the normal text editor html view will
be the same.  Instead of differentiating on the view name in the url, BFG
differentiates on the X-Requested-With header.  (Caveat: I don't do a ton of
AJAX so I'm not entirely sure whose responsibility it is to set that
header--the browser, or the javascript code that's making the request.  If
it's the latter, you may need to check and make sure that TinyMCE sets the
appropriate header.)

In your case, though, I would actually leave it the way you have it.  This
is because, conceptually, to me, the image list is a different resource than
the text editor, so it makes sense to differentiate them by url.  I would
have more of a tendency to use the xhr predicate if I were interested in
providing both an html and a json view for the same logical resource, say a
blog post, or a photograph.  This way the resource is always at the same url
but depending on the client that generated the request you can send back an
html page that displays the resource or json that represents the resource
logically.

As far as renderers go, I don't think you would be served by using BFG's
json renderer because it looks like you're passing back to TinyMCE a
javascript assignment statement.  JSON really only applies to the right hand
of that assignment.  If you were to use the BFG renderer you'd only be
sending back the right side of the assignment.  If TinyMCE also needs the
left side to function, that probably don't work.  I am completely ignorant,
though, of what TinyMCE expects there.

Hope this helps.

Chris




On Fri, Jan 22, 2010 at 6:27 PM, georgehu <geo...@gmail.com> wrote:

>  Hi there,
>
> As I'm fairly new to the python web development, sometimes I found it's
> hard to get an idea of how to use features in the repoze.bfg. For example,
> for the xhr view predicate, the document says "this is useful for detecting
> AJAX requests issued from jQuery, Prototype and other Javascript
> libraries.".
>
> Well yes, I understand it, but I still don't know what can I do with it.
> Although a first look at the question I would say it's not appropriate to
> ask it here, as I can understand this is a feature exists in such a
> framework like BFG, which declares itself as *Minimalism*, and it's
> totally up to each individual developer to deal with, but for a newbie like
> me just can't imagine how to start with it. Thankfully I can study and learn
> many other features by looking at the source code such as BFG site, but
> still there are points here and there which I can't find a example. So a
> sample code snippet would be will very helpful.
>
> Enough talk and let's go back to the business, I'm currently trying to use
> TinyMCE to provide a rich editor of my text field, one of its functionality
> is to supply a list of jpg files on the server to the user, which is an
> option named "external_image_list_url" in TinyMce. My task is to return a
> java script like "var = array of files" to the main TinyMCE script in a
> template, I first defined a view in zcml:
>
> <view
>     for="myproject.interfaces.ITextEdit"
>     name="imgList"
>     view=".js.imgList"
>     />
>
> in the template, I have TinyMCE java script initialized as this,
>
> ......
>  external_image_list_url: "imgList"
> ......
>
> in the js.py I have a function to return a javascript document,
>
> def imgList(context, request):
>     response = Response(content_type='application/x-javascript')
>     here = os.path.dirname(__file__)
>     basedir = os.path.join(here,"templates","static","images","site")
>     baseurl = "/static/images/site/"
>
>     files = glob.glob(basedir+"/*")
>     array = [(os.path.basename(file),baseurl+os.path.basename(file)) for
> file in files]
>
>     response.body = "var %s = %s" % ('tinyMCEImageList',
> simplejson.dumps(array))
>     return response
>
> in the above function I use json to return the array of image files to the
> template,
>
> It runs without problem, my question is, is it a best way to do it? As I
> read through the document, I saw "xhr" view predicate, and json renderer, is
> there a way to utilize them to simplify my code?  And I'm very interested to
> see a sample piece of code to use json+xhr+jquery.
>
> Thanks!
>
>
> _______________________________________________
> Repoze-dev mailing list
> Repoze-dev@lists.repoze.org
> http://lists.repoze.org/listinfo/repoze-dev
>
>
_______________________________________________
Repoze-dev mailing list
Repoze-dev@lists.repoze.org
http://lists.repoze.org/listinfo/repoze-dev

Reply via email to