简体   繁体   English

如何刷新Angular JS部分文件中的第三方小部件的数据

[英]how to refresh data of 3rd party widget in angular js partial files

I am including YOTPO reviews on my angular js website. 我在我的angular js网站上加入了YOTPO评论。 Its a two step process - 1) Include their widget.js - I have included this on my index.html, where all other js files have also been included 2) Include their widget's div tag the page where reviews have to be displayed - I have included this on individual product page, which is a partial/product.html 这是一个两步过程-1)包括他们的widget.js-我已经将其包含在我的index.html中,其中还包括了所有其他js文件2)包括他们的部件的div标签必须在其中显示评论的页面-我已将其包含在单个产品页面(partial / product.html)中

Issue - My site is a search website. 问题-我的网站是搜索网站。 Index.html has a search box, all the results are displayed there. Index.html有一个搜索框,所有结果都显示在其中。 When user clicks on a particular page in results, he gets redirected to particular product page (partial/product.html) 当用户在结果中单击特定页面时,他将被重定向到特定产品页面(partial / product.html)

User Flow - Lets assume, on search I get products P1, P2, P3 in results. 用户流-假设在搜索中我得到了结果P1,P2,P3。

For the first time, If i click on P1 and go to its product page, YOTPO review appears. 第一次,如果我单击P1并转到其产品页面,则会出现YOTPO评论。 But when I go back to search and click on any other result (even with new search query and results) - YOTPO widget doesnot appear. 但是,当我返回搜索并单击任何其他结果时(即使使用新的搜索查询和结果),也不会出现-YOTPO小部件。 But if i refresh the page, it appears. 但是,如果我刷新页面,它就会出现。

I think reviews are displayed when widgets.js is called (which is included in index.html) and refresh reloads it. 我认为在调用widgets.js(包含在index.html中)并刷新重新加载它时会显示评论。 But How do i make sure that its reloaded everytime user navigates to product page? 但是,如何确保每次用户导航到产品页面时重新加载? Or how is this scenario handled in angularJS - I am new to JS, may be I am missing some concept here. 或者在angularJS中如何处理这种情况-我是JS的新手,可能是我在这里缺少一些概念。

Hey I'm working on a React/Redux application so it may not be the same. 嘿,我正在开发一个React / Redux应用程序,所以可能不一样。

When you have the yotpo sitewide script, it should create an object, yotpo (if you console.log(yotpo) you can see it). 当您拥有yotpo全站脚本时,它将创建一个对象yotpo(如果您使用console.log(yotpo),则可以看到它)。 If you look in the proto , there are a number of functions in there. 如果您查看原型 ,则其中有许多功能。

The products are pulled in by the product ID, whether parent or child sku or however you have it. 产品是通过产品ID(无论是父级还是子级sku或是否拥有)来提取的。

I was having an issue where it wasn't updating the widget if we went to a new product, but would render right if I refreshed. 我遇到的一个问题是,如果我们使用新产品,它不会更新小部件,但是如果我刷新,它将正确显示。

This is how I solved it; 这就是我解决的方法; I put this code into the function where it is re-rendering: 我将此代码放入重新渲染的函数中:

// update Yotpo dynamically
  window.yotpo.initialized = false;
  window.yotpo.clean();

// there is a widgets array that the widgets are pulling data from; we need to update this
  for (let i = 0, len = window.yotpo.widgets.length; i < len; i++) {
    window.yotpo.widgets[i].settings.pid = nextProps.product.id;
    window.yotpo.widgets[i].settings.main_widget_pid = nextProps.product.id;
  }
  setTimeout(() => window.yotpo.initWidgets(), 500);

I'm not sure if that fixes your specific issue but hopefully it can at least point you in the right direction. 我不确定这是否可以解决您的特定问题,但希望它至少可以为您指明正确的方向。

For you, you might just have to run window.yotpo.initWidgets() when that div is re-rendered. 对于您来说,当重新渲染该div时,您可能只需要运行window.yotpo.initWidgets()。

I would recommend looking at the "Creating a Directive that Manipulates the DOM" section of this page. 我建议您看一下本页面的“创建操纵DOM的指令”部分。

Angular Directives 角度指令

I am not familiar with YOTPO, but I am willing to guess you could create a updateReview function within the link function of a custom directive that would be called every time you need to update the reviews. 我对YOTPO并不熟悉,但是我想您可以在每次需要更新评论时都会调用的自定义指令的链接函数中创建一个updateReview函数。 That would most likely be an excellent place to start, since now it sounds like the code is only being executed when a certain view is loaded. 那很可能是一个很好的起点,因为现在听起来好像只有在加载某个视图时才执行代码。

For my VueJS SPA I've used the following code in mounted() : 对于我的VueJS SPA,我在mounted()使用了以下代码:

if (
  typeof yotpo != 'undefined'
  && yotpo
  && yotpo.initialized
  && !document.querySelector('.yotpo-display-wrapper')
) {
  (new Yotpo.API(yotpo)).refreshWidgets()
}

found method on https://support.yotpo.com/en/article/loading-yotpo-with-ajax https://support.yotpo.com/en/article/loading-yotpo-with-ajax上找到了方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM