Overriding endpoint response

222 views
Skip to first unread message

jhaagmans

unread,
Apr 16, 2013, 5:35:15 AM4/16/13
to net...@googlegroups.com
Hi all,

I've been trying to implement authentication within Netzke. Now, I've made a nice login form that pops up when a user isn't logged in to the application when accessing it. That works fine and I've also managed to implement the logging in through Devise (although that was a pickle).

What I can't manage to do is displaying a form when the AdminUser session is invalidated. I have the following NetzkeController:

class Admin::NetzkeController < AdminController
  include Netzke::Railz::ControllerExtensions
 
  before_filter :require_admin_user, :except => [:ext] # Ext is needed to be able to use ExtJS for the login form.
 
  private
 
  def require_admin_user

    unless admin_user_signed_in?
     
      params[:act] = 'admin__workspace'
      params[:method] = 'deliverComponent'
      params[:data] = [{name: 'login'}]
     
      render :text => direct_response(params, invoke_endpoint(params)), :layout => false
    end
  end
end

Now I know this won't work for most Netzke calls (like grids or forms) because it expects a different kind of response. But I can't seem to figure out a way to display my login form window. Is there a trick I can use?

I hope you know what I mean.

Regards,
Jaap Haagmans

jhaagmans

unread,
Apr 16, 2013, 9:50:32 AM4/16/13
to net...@googlegroups.com
I already saw that I missed the .to_json on direct_response, which I fixed.

Let me simplify my issue. I want to use a before_filter on my NetzkeController that could simply do this:

def require_admin_user
  unless admin_user_signed_in?
    render :text => direct_response(params, {netzke_feedback: 'Not logged in!'}).to_json, :layout => false
  end
end

Is there any way to hijack any action (including a get_data kind of action) using a method like this?

Thanks.

jhaagmans

unread,
Apr 16, 2013, 10:26:47 AM4/16/13
to net...@googlegroups.com
Hmmm, now that I think of it. It would be nicer if I would simply respond with a 401 error and handle that error somehow at the client side by displaying a login form. Is there a neat way to catch errors?

Max Gorin

unread,
Apr 16, 2013, 10:28:39 AM4/16/13
to net...@googlegroups.com
I'm not sure if there is, but probably should be! I'll have a look into this tomorrow.

--
Max Gorin
@nomadcoder
author of @netzke



16 Apr 2013 20:11
Hmmm, now that I think of it. It would be nicer if I would simply respond with a 401 error and handle that error somehow at the client side by displaying a login form. Is there a neat way to catch errors?

On Tuesday, April 16, 2013 3:50:32 PM UTC+2, jhaagmans wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/3jrM_AXqX5IJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
16 Apr 2013 19:35
I already saw that I missed the .to_json on direct_response, which I fixed.

Let me simplify my issue. I want to use a before_filter on my NetzkeController that could simply do this:

def require_admin_user
  unless admin_user_signed_in?
    render :text => direct_response(params, {netzke_feedback: 'Not logged in!'}).to_json, :layout => false
  end
end

Is there any way to hijack any action (including a get_data kind of action) using a method like this?

Thanks.

On Tuesday, April 16, 2013 11:35:15 AM UTC+2, jhaagmans wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/fIjTxSS6cOgJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
16 Apr 2013 15:20
Hi all,

I've been trying to implement authentication within Netzke. Now, I've made a nice login form that pops up when a user isn't logged in to the application when accessing it. That works fine and I've also managed to implement the logging in through Devise (although that was a pickle).

What I can't manage to do is displaying a form when the AdminUser session is invalidated. I have the following NetzkeController:

class Admin::NetzkeController < AdminController
  include Netzke::Railz::ControllerExtensions
 
  before_filter :require_admin_user, :except => [:ext] # Ext is needed to be able to use ExtJS for the login form.
 
  private
 
  def require_admin_user

    unless admin_user_signed_in?
     
      params[:act] = 'admin__workspace'
      params[:method] = 'deliverComponent'
      params[:data] = [{name: 'login'}]
     
      render :text => direct_response(params, invoke_endpoint(params)), :layout => false
    end
  end
end

Now I know this won't work for most Netzke calls (like grids or forms) because it expects a different kind of response. But I can't seem to figure out a way to display my login form window. Is there a trick I can use?

I hope you know what I mean.

Regards,
Jaap Haagmans
--
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/ofTosJdRt-YJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

jhaagmans

unread,
Apr 16, 2013, 11:02:20 AM4/16/13
to net...@googlegroups.com
Cheers. I've also been looking at the ExtJS docs about this, but I could only find this:

http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.Error

If you know a way to implement this, I would get me 100 steps further. I'm so close to where I want to be with Netzke now!!

Max Gorin

unread,
Apr 16, 2013, 11:04:42 AM4/16/13
to net...@googlegroups.com
Actually, you helped me realize that there was no proper server error handling at all in Netzke - shame on me! I'm half way through fixing it already, will post when I'm done.


--
Max Gorin
@nomadcoder
author of @netzke



16 Apr 2013 20:47
Cheers. I've also been looking at the ExtJS docs about this, but I could only find this:

http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.Error

If you know a way to implement this, I would get me 100 steps further. I'm so close to where I want to be with Netzke now!!

On Tuesday, April 16, 2013 4:28:39 PM UTC+2, nomadcoder wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/ch09bwLOMkkJ.

Max Gorin

unread,
Apr 16, 2013, 11:24:58 AM4/16/13
to net...@googlegroups.com
Here's what we have now (in master). When a server exception occurs, a client-side method "onDirectException" gets called, with an Ext.direct.ExceptionEvent instance passed to it (see Ext JS docs). The default implementation simply gives a warning. However, you can override it globally as explained here: http://writelesscode.com/blog/2013/04/04/changing-behavior-of-all-netzke-components-on-the-client-side/

By analyzing Ext.direct.ExceptionEvent, you can see whether server returned 401 - and handle as you find appropriate.

Thanks for pointing me in this direction - I think, that's a much better way to do what you need, as compared to manually building a proper Netzke response!

Let me know if you have any issues with this.

--
Max Gorin
@nomadcoder
author of @netzke



16 Apr 2013 20:47
Cheers. I've also been looking at the ExtJS docs about this, but I could only find this:

http://docs.sencha.com/extjs/4.0.7/#!/api/Ext.Error

If you know a way to implement this, I would get me 100 steps further. I'm so close to where I want to be with Netzke now!!

On Tuesday, April 16, 2013 4:28:39 PM UTC+2, nomadcoder wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/ch09bwLOMkkJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
16 Apr 2013 20:13
I'm not sure if there is, but probably should be! I'll have a look into this tomorrow.



jhaagmans

unread,
Apr 17, 2013, 8:39:50 AM4/17/13
to net...@googlegroups.com
Hey Max,

This looks like it does exactly what I was asking for, I will probably be able to try this out tomorrow. Thank you very much :)

If this works, I'll try posting an example of my use case to github and heroku this weekend. It might be something to consider adding to the current demo material.

Regards,
Jaap

jhaagmans

unread,
Apr 17, 2013, 12:07:46 PM4/17/13
to net...@googlegroups.com
Okay, I managed to test it today. I've created the following javascript code:

Ext.define(null, {
  override: "Netzke.classes.Core.Mixin",
  onDirectException: function(e) {
      Netzke.warning(e.getTransaction());
  }
});

Now, I get a Transaction object back when calling a deliverComponent action (through a menu item calling netzkeLoadComponent), but when I call my stores (I have a chart and multiple grids), the onDirectException doesn't seem to get called. Is there any reason for that?

Thanks again :)

Kind regards,

Jaap

On Tuesday, April 16, 2013 5:24:58 PM UTC+2, nomadcoder wrote:

jhaagmans

unread,
Apr 18, 2013, 5:09:42 AM4/18/13
to net...@googlegroups.com
I noticed that netzkeProcessEndpoints isn't called for get_data endpoints, but I'm not 100% sure why that is. It seems like Netzke::Basepack::Grid has its own exception handler, but I don't want to use that. I could probably override it, but I'm not sure how. I tried adding a loadExceptionHandler function to my mixed in javascript, but that doesn't seem to override it.

Also, I have a chart using its own store calling to a get_data endpoint, but that's not responding at all when the get_data output isn't what it expects.

I hope you can help.

Max Gorin

unread,
Apr 18, 2013, 5:13:33 AM4/18/13
to net...@googlegroups.com
Yeah, your assumption is correct - Grid uses its own exception handler. I'm looking into it right now, trying to unify that.

But, given I found the way, for your chart store to make use of it, it *must* use the direct proxy (Ext.data.proxy.Direct)! See how it's implemented for Grid in grid.js.


--
Max Gorin
@nomadcoder
author of @netzke



18 Apr 2013 14:54
I noticed that netzkeProcessEndpoints isn't called for get_data endpoints, but I'm not 100% sure why that is. It seems like Netzke::Basepack::Grid has its own exception handler, but I don't want to use that. I could probably override it, but I'm not sure how. I tried adding a loadExceptionHandler function to my mixed in javascript, but that doesn't seem to override it.

Also, I have a chart using its own store calling to a get_data endpoint, but that's not responding at all when the get_data output isn't what it expects.

I hope you can help.

On Wednesday, April 17, 2013 6:07:46 PM UTC+2, jhaagmans wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/ujIKn4m6rZkJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
17 Apr 2013 21:52
Okay, I managed to test it today. I've created the following javascript code:

Ext.define(null, {
  override: "Netzke.classes.Core.Mixin",
  onDirectException: function(e) {
      Netzke.warning(e.getTransaction());
  }
});

Now, I get a Transaction object back when calling a deliverComponent action (through a menu item calling netzkeLoadComponent), but when I call my stores (I have a chart and multiple grids), the onDirectException doesn't seem to get called. Is there any reason for that?

Thanks again :)

Kind regards,
Jaap

On Tuesday, April 16, 2013 5:24:58 PM UTC+2, nomadcoder wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/6bH3dO2bq7gJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
16 Apr 2013 21:09
Here's what we have now (in master). When a server exception occurs, a client-side method "onDirectException" gets called, with an Ext.direct.ExceptionEvent instance passed to it (see Ext JS docs). The default implementation simply gives a warning. However, you can override it globally as explained here: http://writelesscode.com/blog/2013/04/04/changing-behavior-of-all-netzke-components-on-the-client-side/

By analyzing Ext.direct.ExceptionEvent, you can see whether server returned 401 - and handle as you find appropriate.

Thanks for pointing me in this direction - I think, that's a much better way to do what you need, as compared to manually building a proper Netzke response!

Let me know if you have any issues with this.


jhaagmans

unread,
Apr 18, 2013, 6:37:17 AM4/18/13
to net...@googlegroups.com
Yes, I currently use the following Direct proxy for my chart (in chart.js):

           proxy: {
                type: 'direct',
                directFn: Netzke.providers[this.id].getData,
                reader: {
                    type: 'json',
                    root: 'data'
                },
                listeners: {
                    exception: function(proxy, exception, operation) {
                        alert('Error!')
                    }
                }
            }

As you can see, I found a way to catch the exception in the proxy itself, although it isn't as DRY as I'd like it to be.

Cheers!
Jaap

Max Gorin

unread,
Apr 18, 2013, 7:32:56 AM4/18/13
to net...@googlegroups.com
Alright, please, pull from master, and see whether you can remove your exception listener now. Grid also works fine for me now, there was no need to update Basepack for that. Let me know how this worked.

jhaagmans

unread,
Apr 18, 2013, 7:50:46 AM4/18/13
to net...@googlegroups.com
I'll check ASAP, cheers!

jhaagmans

unread,
Apr 18, 2013, 8:16:04 AM4/18/13
to net...@googlegroups.com
My findings:

- In a Grid, I seem to get 8 calls to onDirectException. Why is that?
- In my chart, it doesn't call onDirectException

I'll look into your changes to try and understand.

Max Gorin

unread,
Apr 18, 2013, 8:55:54 AM4/18/13
to net...@googlegroups.com
> In a Grid, I seem to get 8 calls to onDirectException. Why is that?

Hm, is it possible that your app loads multiple grids initially?

jhaagmans

unread,
Apr 18, 2013, 9:15:08 AM4/18/13
to net...@googlegroups.com
It loads just the one, I also see just one server call for just one grid.

jhaagmans

unread,
Apr 18, 2013, 11:00:30 AM4/18/13
to net...@googlegroups.com
I've tried this:

Netzke.directProvider.on('data', function(self, e) {
      // if (Ext.getClass(e) == Ext.direct.ExceptionEvent) {
        // this.onDirectException(e);
      // }
        console.log(JSON.stringify(Ext.getClass(e)))
    }, this);

But this logs "undefined" a couple of times for each call. Also for the chart call, so I hope I'm on the right track by trying to evaluate the class of e (I'm guessing it's not an Ext.direct.ExceptionEvent). I'm just not sure how that would be done.

Cheers.

Max Gorin

unread,
Apr 18, 2013, 10:55:55 PM4/18/13
to net...@googlegroups.com
Jaap,

Apparently, it was a bad idea to subscribe to that event from each component - that's the reason you were getting multiple calls to the handler.

I removed that for now, and suggest a different, simpler approach:

I added the 'serverexception' event to Netzke.directProvider. Simply subscribe to it from within the initComponent method of your top-level (application?) component:

  initComponent: function() {
    // handle server exceptions
    Netzke.directProvider.on('serverexception', function(self, e) {
      console.log("Caught ya!");
    }, this);

    this.callParent();
  }


I also updated Basepack for the Grid to not shout about the server exception.

Thanks for your patience and feedback!


--
Max Gorin
@nomadcoder
author of @netzke



18 Apr 2013 20:45
I've tried this:

Netzke.directProvider.on('data', function(self, e) {
      // if (Ext.getClass(e) == Ext.direct.ExceptionEvent) {
        // this.onDirectException(e);
      // }
        console.log(JSON.stringify(Ext.getClass(e)))
    }, this);

But this logs "undefined" a couple of times for each call. Also for the chart call, so I hope I'm on the right track by trying to evaluate the class of e (I'm guessing it's not an Ext.direct.ExceptionEvent). I'm just not sure how that would be done.

Cheers.

On Thursday, April 18, 2013 3:15:08 PM UTC+2, jhaagmans wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/RbW3pbBYy64J.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
18 Apr 2013 19:00
It loads just the one, I also see just one server call for just one grid.

On Thursday, April 18, 2013 2:55:54 PM UTC+2, nomadcoder wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/2Vrt-dMi6lgJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
18 Apr 2013 18:01
My findings:

- In a Grid, I seem to get 8 calls to onDirectException. Why is that?
- In my chart, it doesn't call onDirectException

I'll look into your changes to try and understand.

On Thursday, April 18, 2013 1:50:46 PM UTC+2, jhaagmans wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/1Ag3WKGHyywJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
18 Apr 2013 17:35
I'll check ASAP, cheers!

On Thursday, April 18, 2013 1:32:56 PM UTC+2, nomadcoder wrote: --
You received this message because you are subscribed to the Google Groups "Netzke" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netzke+un...@googlegroups.com.
To post to this group, send email to net...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/netzke/-/mv87TpMCrS4J.

For more options, visit https://groups.google.com/groups/opt_out.
 
 
18 Apr 2013 17:17

jhaagmans

unread,
Apr 19, 2013, 5:34:46 AM4/19/13
to net...@googlegroups.com
I don't know why this also works for my chart, but it does! I also love the way it works!

Thanks very much :)

jhaagmans

unread,
Apr 26, 2013, 9:28:27 AM4/26/13
to net...@googlegroups.com
It's me again ;)

When using netzkeLoadComponent, a loading mask is set. How can I determine, after returning a 403 response, which components are being loaded so I can remove the loading mask and store the components in the component_session? this.componentsBeingLoaded returns an empty hash.

jhaagmans

unread,
Apr 26, 2013, 10:21:52 AM4/26/13
to net...@googlegroups.com
Most of the times, it's my menu component that's loading another component, so I now have this:

var menuComponents = this.netzkeGetComponent('menu').componentsBeingLoaded
          for(component in menuComponents) {
            var storedConfig = menuComponents[component]
            if (storedConfig.loadMaskCmp) {
                storedConfig.loadMaskCmp.hide();
                storedConfig.loadMaskCmp.destroy();
            }
            delete this.componentsBeingLoaded[component];
        }

If there's a better way, I'd love to hear it :)

Max Gorin

unread,
Apr 26, 2013, 10:00:53 AM4/26/13
to net...@googlegroups.com
Jaap,

Hope, this piece of code helps:

initComponent: function() {
Netzke.directProvider.on('serverexception', function(e) {
var compId = e.getTransaction().action,
cmp = Ext.getCmp(compId); // components that instantiated the
failed loading
console.log("cmp.componentsBeingLoaded:",
cmp.componentsBeingLoaded);
})
this.callParent();
}

Works for me.

Although I think it should be responsibility of Core to remove those
loading masks in case of failed component loading. I'll add that as an
issue.

Max Gorin

unread,
Apr 26, 2013, 10:53:14 AM4/26/13
to net...@googlegroups.com
> If there's a better way, I'd love to hear it :)

Yes, there is as of now :) - try the latest master. I decided to fix
this masking thing right away instead of pushing it to the issues.

You still can use the code from my last email if you need to, but now
the actual cleaning-up during component loading is done for you.

Let me know if you encounter problems (also see the changelog).

jhaagmans

unread,
May 13, 2013, 6:09:39 AM5/13/13
to net...@googlegroups.com
Wow super, I'll check this out today. Sorry for the delayed response, I was working another project with higher priority.

Max Gorin

unread,
May 13, 2013, 9:45:58 AM5/13/13
to net...@googlegroups.com
Glad to have you back!

Please, give me another day before you dig into this, so that I can fix
a problem in the Netzke Core master related to nested component loading.
I'll try to push a fix into it asap.

Cheers,
Reply all
Reply to author
Forward
0 new messages