unobtrusive-javascript

Rails :confirm modifier callback?

不想你离开。 提交于 2019-12-03 05:54:18
I want to call a javascript function that will be conditional upon a user's response to the confirm box. For example, I have the following anchor: <%= link_to 'Sign Out', destroy_session_path, confirm: 'Are you sure that you would like to sign out?', method: :delete %> Should I just abandon the unobtrusive javascript and bind my own jQuery callback to my anchor? Or is there some magical way to pass a callback function to the Rails url helper? I did not see any discussion of callbacks in the documentation . By following the link_to source, I see that the :confirm modifier is passed to the

Unobtrusive Knockout

别来无恙 提交于 2019-12-03 02:40:45
问题 I've recently got up to speed with Knockout and I think it's a fantastic framework. However I have one concern. I'm finding that in non-trivial binding cases, I have snippets of javascript code creeping into my view (markup). In fact quite a few code samples in the Knockout documentation demonstrate this too. Does this make Knockout inherently obtrusive? Should I just accept it for what it is and not be worried in practice? Or are there any patterns/techniques I should be employing to make

Event binding on dynamically created elements?

◇◆丶佛笑我妖孽 提交于 2019-12-02 17:26:20
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off . This happens on page ready and works just fine. The problem I have is that any select boxes I add via Ajax or DOM after the initial loop won't have the event bound. I have found this plugin (jQuery Live

Unobtrusive Knockout

给你一囗甜甜゛ 提交于 2019-12-02 16:13:33
I've recently got up to speed with Knockout and I think it's a fantastic framework. However I have one concern. I'm finding that in non-trivial binding cases, I have snippets of javascript code creeping into my view (markup). In fact quite a few code samples in the Knockout documentation demonstrate this too. Does this make Knockout inherently obtrusive? Should I just accept it for what it is and not be worried in practice? Or are there any patterns/techniques I should be employing to make Knockout unobtrusive? Michael Berkompas Great question. I've been writing complex KnockoutJS views for

Event binding on dynamically created elements?

谁说胖子不能爱 提交于 2019-12-02 13:44:34
I have a bit of code where I am looping through all the select boxes on a page and binding a .hover event to them to do a bit of twiddling with their width on mouse on/off . This happens on page ready and works just fine. The problem I have is that any select boxes I add via Ajax or DOM after the initial loop won't have the event bound. I have found this plugin ( jQuery Live Query Plugin ), but before I add another 5k to my pages with a plugin, I want to see if anyone knows a way to do this, either with jQuery directly or by another option. Popnoodles As of jQuery 1.7 you should use jQuery.fn

How to self-identify the position of a script's tag in the DOM?

微笑、不失礼 提交于 2019-12-02 07:34:49
问题 There may be a better way of doing this, but currently I have an nicely encapsulated, JavaScript object which can have some configurable options. I include a chunk of HTML code on a page (via Dreamweaver 'snippets'), and on page load my JS file runs through the DOM and identifies any of those code chunks as a particular functionality, and goes ahead and sets that functionality up. That's all fine up until I wish to add more than one object on a page, and have them be configurable. The

How to tell Ajax.ActionLink OnSuccess callback which element initiated the ajax

房东的猫 提交于 2019-12-01 04:30:35
I want my razor view to look something like this @Ajax.ActionLink("A", "Buy", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "updateLetter" }, new { id = "letter-A" }) @Ajax.ActionLink("B", "Buy", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "updateLetter" }, new { id = "letter-B" }) @Ajax.ActionLink("C", "Buy", new AjaxOptions() { HttpMethod = "Post", OnSuccess = "updateLetter" }, new { id = "letter-C" }) and my javascript to look something like this function updateLetter(letter) { $("#letter-" + letter).toggleClass('selected'); } the idea being that if I click the A link, it'll

show div if unobtrusive validation was invalid and hide it if Valid in MVC 3

时光总嘲笑我的痴心妄想 提交于 2019-11-30 22:13:59
This is a part of my Edit view: <dt> @Html.LabelFor(model => model.MainModel.StartDate) </dt> <dd> @Html.TextBoxFor(model => model.MainModel.StartDate) @Html.ValidationMessageFor(model => model.MainModel.StartDate) <div class="targetDiv"> My content </div> </dd> So as all of you know when StartDate field in my model not valid unobtrusive show the error message and if valid hide it. Now I want to add another action to this process. I need if StartDate value is Invalid show "targetDiv" div and if StartDate value is Valid hide it. what is your suggestion? You can check for field validity with

How to use asp.net mvc 3 jquery validate with a jquery dialog that does an ajax submit?

耗尽温柔 提交于 2019-11-30 20:31:03
I am using asp.net mvc 3 jquery validate unobstructive javascript. I am trying to write all my validation on the serverside through annotations and then have the new feature of mvc 3 take care of the client side. I have a dialog that has a button on it(just a button not a submit button) that I want to post data to the server through ajax. So when the user clicks on the button I do a form submit and return false to cancel the post back. I thought that would trigger the validation but that does not seem to be the case. How do I make the client side validation trigger? Edit <form method="post" id

Reloading partial in an rails app

我是研究僧i 提交于 2019-11-30 07:29:48
I want to reload a partial every 3 seconds on a 'new'-view in my rails app. I have this in my new.html.erb <h1>Controller#new</h1> This is my static content <%= render partial: 'dynamic' %> More Static content How do I get this partial reloaded every 3 seconds? Do I have to use unobtrusive javascript for this? How can I achieve this through ujs? Kashiftufail Put partial in div <div class="dynamic"><%= render partial: 'dynamic' %></div> You can do it by using jquery like this $(document).ready( function() { setInterval(function() { $('.dynamic').load('/controller_name/action_name'); }, 3000); }