Is there a way to make a persistent button without view in discord.py? - discord.py

I create buttons manually like that:
class But(Button):
def __init__(self,label,style,id):
super().__init__(label=label,style=style, custom_id=id)
async def callback(self,interaction):
pass
Because I want to set all parameters like label,style and id when creating a button. But https://github.com/Rapptz/discord.py/blob/master/examples/views/persistent.py example here shows only way to make persistent views. Is there a way to make buttons, not nested into the view persist?
I tried to make persistent view and add buttons to it, but then only buttons nested in this view persist:
async def setup_hook(self) -> None:
self.add_view(persist())
class persist(View):
def __init__(self):
super().__init__(timeout=None)
#discord.ui.button(label="Test persistence", style=discord.ButtonStyle.red,custom_id="persistent_view")
async def callback(self,interaction,button):
await interaction.response.send_message("Persists!")
view = persist()
button1 = But(label=label,style=style, id='1')#I get style and label in slash command
view.add_item(button1)
await interaction.response.send_message("text",view=view)

The only other way, which I employ myself, is to use the on_interaction event.
As the name implies, the event fires whenever an interaction is triggered.
However, even then, you will need a way to recognize the interaction so as to react in the appropriate manner to it, which might be difficult with an entirely dynamic id

Related

Appcelerator - Notify parent view if something happen on child view

How should I get info about child view's events on parent view?
For example:
I pass and argument to the child (Alloy.createController('myChildView', { info: test }).getView()). Then I work with it and set a global variable from false to true (Alloy.Globals.childPrecessed = true). After that, I can spent any time on this view, but when I click on a button which fires a hide event, I should process the info from parent view.
My first thought was I fire az appwide event (myChildHide), and listen for it in the parent view. If I catch it, then I process the info, and destroy the listener...
Is this the best method? I'm not sure...
Has anybody better solution for this?
Thanks!
I am a fan of event listeners, so I think your approach is a good one.
What I normally do is to initiate the event listener right before I need it to be effective, i.e. in the method opening the child window. But first I use backbone events for simple event triggering. See Fokke Zandbergen's article for further info. So assuming you have set up a "dispatcher" then I would do something like this:
function openChild(){
dispatcher.on("child-calling", doChildsWork);
// ... open child view
}
Then in the doChildsWork I would disable the event handler once called:
function doChildsWork(args){
dispatcher.off("child-calling");
// ... do work initiated by child view using args...
}
And finally in the child view (assuming you have set up a "dispatcher") you would do something like this:
function doChildsWork(){
// ... Tell parent to do some work
dispatcher.trigger("child-calling",{test:true});
// ... continue whatever is going on in child
}
I use this approach a lot - and it works well :-)
/John

Processing child Control Events in Windows Forms Components in C++

I am a noob to Windows Forms so this is likely a remedial question. I have a child component with a button and a text field. I want to use multiple instances of these in a parent component or form. At runtime, when the user clicks one of the buttons, I want the parent to get the event to decide what to do with the associated text.
Coming from the long lost world of Borland C++ Builder, during design time, I would simply double-click on the buttons and handlers in the parent would be created which I could just elaborate the code. With Windows Forms, the component controls are not clickable (at design time) from the parent and are "frozen". It is not obvious to me how to pass any child button clicks to a parent. I've tried things like changing the button modifier from private to public but that doesn't help. How is this best accomplished.
Note I am using C++ as I am sharing header file definitions with an associated C++ embedded app.
-Bob
UPDATE:
My apologies, I thought I was still in my C# search :(
This is slightly complicated if you actually want to bubble up an event, or very easy if you use methods.
Methods:
In your child container, add a constructor or property that takes in and stores the parent. Then in the button handler, call this.Parent.ButtonClicked(this); and of course in the parent, add a ButtonClicked(ChildType child) method. That should get you there that way.
Custom Event:
To use events, you need to add a few things. Firstly, add a custom EventArgs class as such:
class ChildClickedEventArgs : EventArgs
{
// include a ctor and property to store and retrieve the child instance.
}
Then add a public delegate for it:
public delegate void ChildClickedEventHandler(object sender, ChildClickedEventArgs e);
Then to your child class, add a ButtonClicked event:
public event ChildClickedEventHandler ChildClicked;
And finally, a helper method to raise it:
private OnButtonClicked()
{
if (this.ChildClicked != null)
{
this.ChildClicked(this, new ChildClickedEventArgs(this));
}
}
Then when you add the child class to the parent, add an event handler for each, and you can handle your custom event for your custom control.
Alternatively:
If you can expose the Button in your child class, simply do the above but register it to this.child.Button.Clicked, saving adding the event handler yourself.

Django editing a user generated object

I'm trying to figure out how to do an inline editing for a user-generated object, what the rough procedure (no code just steps), and whether or not there's some way to do this without AJAX - of course it wouldn't be "inline" anymore.
Say the user object is just 1 line of text and 1 image. Something like,
class UserObject(models.Model):
text = models.CharField()
image_path = models.CharField()
If I were to use AJAX, would this be how it'd go? (sorry this is vague, I can figure out the details just trying to see if I understand the concepts correctly)
Create a form, populate it with an instance of the object belonging to the current user
Next to the image, I'd have, say, a "remove" button, which triggers an AJAX call to a URL that's something like project/remove/ab12345 that's connected to a view that handles it.
Wait for the AJAX call to be done
Then somehow remove the image and buttons, maybe by just deleting the div that contains it all
Is that right??
Also, what if I don't want to use AJAX? Would it go something like this?
Create a form, populate it with an instance of the object belonging to the current user
Next to the image, I'd have, say, a "remove" button, which directly links to the URL that's something like project/remove/ab12345 that's connected to a view that handles it
After the view deletes the image, it goes back to the editing page, which just refreshes and the image is no longer there.
Any pointers would be greatly appreciated!! I can figure out the details of the coding, just wondering if I am getting the concepts right.
An object created by a user is really no different from one you create yourself (except you have to be suspicious of potentially malicious input!). The simplest way to be able to edit objects outside the admin interface is to use the built-in UpdateView. Similarly you can delete them with a DeleteView. If you want to restrict you can edit objects you can user the PermissionRequiredMixin from django braces.
OK since I posted this ultra-vague question I'm going to answer it.
AJAX-free:
The AJAX free version is pretty much as I described, create a view and a URL that deletes the image and goes right back to the referring page. Next going to try the AJAX version, which basically requires a view that returns some kind of a signal of failure or success.
urls.py
url('^project/remove_image/(?P<image_id_string>[0-9A-Fa-f]+)/$', pbrand.views.ProjectRemoveImageView.as_view(), name='project_remove_image'),
views.py
class ProjectRemoveImageView(View):
redirect_url = '/project/edit' # the editing url
def get(self, request, image_id_string, *args, **kwargs):
# ... some checks on permissions
image.delete()
return HttpResponseRedirect(self.redirect_url + "/" + project.id_string)
inside the template
<a class = "btn btn-default btn-sm" href="{% url 'project_remove_image' i.id_string %}" role="button">remove</a>

Handling Asynchronous operations in Windows Phone7

I am developing a Windows Phone7 application in which I have two App bar buttons both when clicked makes Asynchronous calls to Web and Callbacks will be performed upon the Web response.
Now my problem is, if I click on one button and as the Async operation is going on in the background ans meanwhile if I click on another button both callbacks are executing one after the other, which is not good for obvious reasons. Could any one help me on how to handle this???
First I thought to disable other buttons when 1 Async operation is going. But it doesnt give good feel for user. So what will be the best way to handle this problem??
You can use a Flag variable and check its value within the async call complete method. Based on your requirement you can choose to update or not update the view.
I was looking for the same answer.
I have found the solution, If you initialize an object inside a constructor like this, you will get multiple loops when you call a service function:
public partial class MainPage : PhoneApplicationPage
{
MovieServiceClient mov;
public MainPage()
{
mov = new MovieServiceClient(); //Don't do this.
InitializeComponent();
}
}
Avoid that.

Removing events in Shoes

The is any way to remove the events in Shoes? I searched around many websites and references but I can't found any way to remove a event... I minding that I will need to create my own event manager... its really nescessary? or there is a way to desattach event listeners?
I have found that Shoes only allows a single listener method on a given event, so you can remove a previous listener by calling the event and not passing a block to it.
For example, this Shoes app will clear the click event after it is clicked one time. If you remove the call to click inside the block, then it will fire repeatedly.
Shoes.app
#p = para "Empty"
#click_count = 0
click do |b,x,y|
#click_count += 1
#p.replace "Clicked #{#click_count} time(s)."
click # remove the click handler
end
end
Which keybindings do you want to remove? All events on the Shoes app or just the default bindings?
If you want to override bindings reserved for shoes like "alt-/", "alt-.", "alt-?", paste the following code into the file which contains your application code
class Shoes::App
def Shoes.show_log # gets called on alt-/
end
def Shoes.show_manual # gets called on alt-?
end
def Shoes.show_selector # gets called on alt-.
end
end
The above code monkey-patches the shoes code and in-turn does nothing when the corresponding keys are pressed.
You can use the same technique for rest of the default bindings. grep the shoes source for the key bindings, find the corresponding method and define an empty method within your app to override the built-in method.
Have you tried method.unbind(obj)?
Can you clarify what you mean by "remove a event"? Are you wanting to ignore a specific event or are you wanting to "turn off" an event listener?
If the former, I'd suggest writing a listener that just ignore the event.
If the later, why not make the body of the listener conditional on some externally accessible value, giving yourself an on/off switch.
If you are wanting something else, edit the question to clarify and I'll stop back later and edit my answer.
In response to your comment, I'd re-suggest the second of the above alternatives. If you wanted to get really fancy you could write something like this:
$keypress_listeners = {}
keypress do |key|
$keypress_listeners.values.each { |l| l.call(key)
end
$keypress_listeners[:hero_controller] = lambda { |key| ... }
:
:
$keypress_listeners.delete[:hero_controller]
and likewise for any other events, but that is probably overkill. On the other hand, it would give you total control of the event processing.

Resources