简体   繁体   中英

How to receive property from control to model CanJS (define modules)

I have Control with configurable defaults parameters:

   define([
  'jquery',
  'can',
  'moment',
  'controls/base',
  'mustache!./becauseYouShopped_view',
  './becauseYouShopped_model',
  'coreMods/clickHistory/v2/clickHistory_model',
  'coreMods/addToFavorite/addToFavorite_control',
], function ($, can, moment, BaseControl, becauseYouShoppedView, BecauseYouShoppedModel, ClickHistoryModel, AddToFavorite) {
  var startDate = moment().subtract(90, 'days').format('YYYY-MM-DD'),
    endDate = moment().add(1, 'days').format('YYYY-MM-DD');

  var BecauseYouShopped = BaseControl({
    defaults: {
      moduleConfigName: 'becauseYouShopped',
      hideRebatePrefix: true,
      rebateAdditionalPrefix: 'Shop and',
      useOldRebateValueWithoutCurrency: true,
      tiered: {
        rebateAdditionalPrefix: 'Shop and earn',
        useOldRebateValueWithoutCurrency: true
      },
      apiParams: {
        start_date: startDate,
        end_date: endDate,
      },
      headingPrefix: 'Because you shop at',
      dataLimitForGetSimilarMerchant: 14,
    }
  }, {
    init: function (element, options) {
      ClickHistoryModel.findOne(options.apiParams).done(function (data) {
        var memberClicksAndOrders = data.attr().response;

        if (memberClicksAndOrders.length) {
          this.shoppedMerchantsInfo = this.getRecentlyShoppedMerchantName(memberClicksAndOrders);

          if (this.shoppedMerchantsInfo) {
            this.getRecommendedMerchantsAndRender();
          }
        }
      }.bind(this)).fail(function (error) {
        mn.log(error);
      });
    },
  return BecauseYouShopped;
});

And I need to export this parameter dataLimitForGetSimilarMerchant: 14, to the Model of this Control for use it in getSimilarMerchantData function. But when I importing it - in way as You can see on the code here:

   define(['jquery', 'can', 'utils/promiseUtils', 'models/base', 'controls/base', 'coreMods/bonusComponents/becauseYouShopped/becauseYouShopped_control',], function ($, can, promiseUtils, BaseModel, BaseControl, BecauseYouShopped) {
  console.log("1 log: ", BecauseYouShopped);
  return BaseModel.extend({
    /**
     * Get response from one content group and type
     * @param contentGroupIdAndType
     * @returns {*}
     */
    getSimilarMerchantData: function (merchantId, merchantName) {
      var deferred = $.Deferred(),
        self = this;
      var controlOptions = BecauseYouShopped.defaults.dataLimitForGetSimilarMerchant;
      console.log("2 log: ", BecauseYouShopped);
      if (!merchantId) {
        return can.Deferred().reject('merchant id is not specified');
      }

      var options = {
        url: mn.contentAPIEndPoint + '/merchants/' + merchantId + '/similar',
        method: 'GET',
        dataType: 'json',
        headers: self.getAjaxHeaders(),
        data: self.getDefaultAjaxData({limit: controlOptions})
      };

  }, {});
});

I received that BecauseYouShopped is undefined

So, how can I import this parameter to the model?

You have the BecauseYouShopped control importing the BecauseYouShopped model, but the model is also importing the control. This sort of circular dependency can sometimes be resolved by your dependency manager if you are not trying to use the dependency module in-thread -- StealJS does this, for example -- but if you are using RequireJS like it seems you are based on the use of define(), you will need to require() one of your dependencies late rather than define()'ing it on initial load. More on this at https://requirejs.org/docs/api.html#circular

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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