javascript - Overriding difficult view model - Stack Overflow

I am trying to replace some text in an input field using JS but the view model overrides my mands each

I am trying to replace some text in an input field using JS but the view model overrides my mands each time. This is the HTML I start with:

<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
    <input type="hidden" name="product[variants][][price]" id="product_variants__price" value="25.00" bind="price" data-dirty-trigger="true">
    <input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric" bind-event-focus="onFocus(this)" bind-event-blur="onBlur(this)" bind-event-input="onInput(this)">
</td>

I run this JS:

jQuery('#product_variants__price').siblings().removeAttr('bind-event-focus');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-input');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-blur');
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("34.00");
jQuery('#product_variants__price').val("34.00");

And I'm left with the following HTML:

<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
    <input type="hidden" name="product[variants][][price]" id="product_variants__price" value="34.00" bind="price" data-dirty-trigger="true">
    <input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric">
</td>

The problem is that each time I click the input field the value is reverted to what it was when the page loaded.

I've also tried running the mand in the parent td along with my value change, to simulate the editing of a variant and preventing default with no success:

jQuery('#product_variants__price').siblings().bind('input', function (e) {
    e.preventDefault();
    return false;
});
jQuery('#product_variants__price').siblings().bind('focus', function (e) {
    e.preventDefault();
    return false;
});
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("£34.00");
jQuery('#product_variants__price').val("£34.00");
jQuery('#product_variants__price').siblings().keydown()

Parent td function:

new Shopify.EditVariantPrice(jQuery('#product_variants__price').parent())

So how can I successfully edit this value in the inputs and also update the Shopify view model?

You can try this for yourself by going here:

login [email protected] password shop1

EDIT: I've tried to find the view model on the page but with no success. Plus, there are no network calls when editing the values in the input fields, leading me to believe the values are being pulled back from somewhere on page.

I am trying to replace some text in an input field using JS but the view model overrides my mands each time. This is the HTML I start with:

<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
    <input type="hidden" name="product[variants][][price]" id="product_variants__price" value="25.00" bind="price" data-dirty-trigger="true">
    <input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric" bind-event-focus="onFocus(this)" bind-event-blur="onBlur(this)" bind-event-input="onInput(this)">
</td>

I run this JS:

jQuery('#product_variants__price').siblings().removeAttr('bind-event-focus');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-input');
jQuery('#product_variants__price').siblings().removeAttr('bind-event-blur');
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("34.00");
jQuery('#product_variants__price').val("34.00");

And I'm left with the following HTML:

<td class="new-variants-table__cell" define="{ editVariantPrice: new Shopify.EditVariantPrice(this) }" context="editVariantPrice" style="height: auto;">
    <input type="hidden" name="product[variants][][price]" id="product_variants__price" value="34.00" bind="price" data-dirty-trigger="true">
    <input class="mock-edit-on-hover tr js-no-dirty js-variant-price variant-table-input--numeric">
</td>

The problem is that each time I click the input field the value is reverted to what it was when the page loaded.

I've also tried running the mand in the parent td along with my value change, to simulate the editing of a variant and preventing default with no success:

jQuery('#product_variants__price').siblings().bind('input', function (e) {
    e.preventDefault();
    return false;
});
jQuery('#product_variants__price').siblings().bind('focus', function (e) {
    e.preventDefault();
    return false;
});
jQuery('#product_variants__price').siblings().focus()
jQuery('#product_variants__price').siblings().val("£34.00");
jQuery('#product_variants__price').val("£34.00");
jQuery('#product_variants__price').siblings().keydown()

Parent td function:

new Shopify.EditVariantPrice(jQuery('#product_variants__price').parent())

So how can I successfully edit this value in the inputs and also update the Shopify view model?

You can try this for yourself by going here:

https://jebus333.myshopify./admin/products/2521183043

login [email protected] password shop1

EDIT: I've tried to find the view model on the page but with no success. Plus, there are no network calls when editing the values in the input fields, leading me to believe the values are being pulled back from somewhere on page.

Share Improve this question edited Sep 10, 2015 at 12:09 forgetso asked Aug 27, 2015 at 14:46 forgetsoforgetso 2,53419 silver badges37 bronze badges 5
  • Wow, you have time typing all those jQuery(...)s? Why not $(...)? They are the same thing! – Daniel Cheung Commented Aug 27, 2015 at 14:55
  • I can't remember why I did it like that. It was probably because I was working on another site on which $ referred to the Chrome Command Line API but I can see on Spotify's web site it does indeed refer to jQuery. – forgetso Commented Aug 27, 2015 at 14:56
  • bind was pretty much deprecated in JQ 1.7. – Dave Newton Commented Aug 27, 2015 at 14:57
  • @forgetso use "stopImmediatePropagation()" and " stopPropagation()" and check – yugi Commented Sep 3, 2015 at 14:01
  • @yugi could you expand on this method? I've tried both methods but not had any luck. – forgetso Commented Sep 8, 2015 at 18:19
Add a ment  | 

3 Answers 3

Reset to default 3 +50

Try this:

var old = Shopify.EditVariantPrice.prototype.onFocus;
Shopify.EditVariantPrice.prototype.onFocus = function(t) { 
  this.price = '50.00'; // Use the price you want here
  old.call(this, t); 
};
jQuery('#product_variants__price').siblings().triggerHandler("focus");
jQuery('#product_variants__price').siblings().triggerHandler("blur");

If it works for you, it's possible that the following will be sufficient:

Shopify.EditVariantPrice.prototype.onFocus = function(t) { 
  this.price = '50.00'; // Use the price you want here
};

Well, there is a kind of a dirty solution...

First of all you'll need a sendkeys plugin. In fact that means you'll need to include this and this JS libraries (you can just copy-paste them in the console to test). If you don't want to use the first library (I personally find it quite big for such a small thing) you can extract only the key things out of it and use only them.

The next step is creating the function which is going to act like a real user:

function input(field, desiredValue) {
  // get the currency symbol while value is still pristine
  var currency = field.val()[0];

  // move focus to the input
  field.click().focus();

  // remove all symbols from the input. I took 10, but of course you can use value.length instead
  for (var i = 0; i < 10; i++) field.sendkeys("{backspace}");

  // send the currency key
  field.sendkeys(currency);

  // send the desired value symbol-by-symbol
  for (var i = 0; i < desiredValue.length; i++) field.sendkeys(desiredValue[i]);
}

Then you can simply call it with the value you wish to assign:

input($("#product_variants__price").next(), "123.00");

I did not really manage to fake the blur event because of lack of the time; that is why I was forced to read the currency and pass .00 as a string. Anyway you already have a way to go and a quite working solution.

Looks like you're trying to automate editing of variant prices of products in Shopify's admin panel.

Instead of playing around with the DOM of Shopify's admin page, I'll suggest using Shopify's bulk product editor which lets you set prices of all variants in a single screen. I feel that you'll have better luck setting the variant prices using JavaScript on the bulk product editor page.

Clicking on the 'Edit Products' button as shown in the screenshot below will open the bulk product editor.

Also check if browser based macro recording plugins like iMacro can be of your help (you can also code macros with JS in iMacro).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744342334a4569477.html

相关推荐

  • javascript - Overriding difficult view model - Stack Overflow

    I am trying to replace some text in an input field using JS but the view model overrides my mands each

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信