1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Lack of Interest Additional js phrases

Discussion in 'Closed Suggestions' started by xf_phantom, Feb 17, 2014.

  1. xf_phantom

    xf_phantom Well-Known Member

    see post 2.
     
    Last edited: Feb 18, 2014
  2. xf_phantom

    xf_phantom Well-Known Member

    A probably better alternative would be a way to fetch the phrases via ajax and add them to XenForo.phrases for later usage

    e.g.
    Code:
    phrasesHelper : function($phrases){
                $phrasesToFetch = [];
                $.each($phrases, function(id,$name){
                    if (!XenForo.phrases[$name]){
                        $phrasesToFetch.push($name);
                    }
                    XenForo.ajax('index.php?core/fetchPhrases',$phrasesToFetch,$.context(this, 'loadSuccess'));
                });
            },
    
    

    then we would need to call only
    Code:
    var neededPhrases = ['save','fat_load_more','cancel'];
    
    XenForo.phrasesHelper(neededPhrases);
    inside our JS code and wouldn't need to change the templates
     
  3. Chris D

    Chris D XenForo Developer Staff Member

    I'm not sure how necessary a new solution is.

    You can just do this in any template:

    Code:
    <script>
        jQuery.extend(XenForo.phrases,
        {
            test_phrase: "TESTING PHRASE"
        });
    </script>
    upload_2014-2-18_13-40-26.png

    You could also create an external JS file and require it in your template wherever you need it.
     
    Adam Howard, HWS and Jeremy like this.
  4. xf_phantom

    xf_phantom Well-Known Member

    This way i can't use the xenforo phrases system the customize the text and it wouldn't work for boards with more languages (and i'm not interessted in creating and rebuilding the file)

    Code:
    <script>
        jQuery.extend(XenForo.phrases,
        {
            test_phrase: "TESTING PHRASE"
        });
    </script>
    requires, that you know exactly, what phrases you'll need in your template(if you want to avoid to load them global) and it makes asynch. javascript loading imposible (and it also makes development easier, because you don't need to maintain the templates:D )
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    Fetch the phrases dynamically in code and insert them into your templates.
     
  6. xf_phantom

    xf_phantom Well-Known Member

    I can't have templates, it's for elements created via js code
     
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    The JS runs on specific pages, yes? They have templates, right? Chris' method works perfectly fine and can be utilized dynamically by inserting them via code.
     
    Chris D likes this.
  8. xf_phantom

    xf_phantom Well-Known Member

    no:) (asynch js loading)
    no:)

    the elements are created via javascript
    e.g.
    Code:
      this.$link = $('<a href="javascript:" />').text(XenForo.phrases.fat_load_more).addClass('button').click($.context(this, 'click')).xfInsert('appendTo', this.$container);
     
    Last edited: Feb 18, 2014
  9. xf_phantom

    xf_phantom Well-Known Member

    XenForo is doing something similar, e.g.

    Code:
      var processing = XenForo.phrases['processing'] || 'Processing',
             cancel = XenForo.phrases['cancel'] || 'Cancel',
             cancelling = XenForo.phrases['cancelling'] || 'Cancelling';
    but i have to avoid hardcoded phrase in the file because of
     
  10. Jeremy

    Jeremy XenForo Moderator Staff Member

    I know what asynchronous code is. I wrote it for a living. If you don't want to use the solution Chris and I have provided, that is up to you. You can wait and see if this suggestion is implemented.
     
  11. cclaerhout

    cclaerhout Well-Known Member

    I may be wrong, but I don't see any asynch js code here.

    From where are your calling the JS that will have the code below ?
    Code:
    this.$link = $('<a href="javascript:" />').text(XenForo.phrases.fat_load_more).addClass('button').click($.context(this, 'click')).xfInsert('appendTo', this.$container);
    
    Extend phrases
    • If the trigger is inside a template, you don't need it, you use directly the XenForo phrase tag.
    • If the trigger is an element in the page that calls a ajax request, then your code has a destination. If this destination is a XenForo url, then it will have a "view", if it has a view, you can use the "renderJson" function to add any variable to the json answer. You will be able to get that element in the "ajaxData" argument from the ajax success function.

      Code:
      Element on the page => ajax request => create/extend view of the request destination => add phrases => ajax request success => find your phrase inside the ajax data
    If you don't want to load gobally a phrase that will only be use in some sections of the website, include at least a template in these sections. You can then create a new JS object with the phrases you need or, if you only need 1 phrase, then put it in a "data" parameter of an element:
    HTML:
    <div data-myPhrase="{xen:phrase myPhrase}"></div>

    About your code
    Code:
    phrasesHelper : function($phrases){
    $phrasesToFetch = [];
    $.each($phrases, function(id,$name){
    if (!XenForo.phrases[$name]){
    $phrasesToFetch.push($name);
    }
    XenForo.ajax('index.php?core/fetchPhrases',$phrasesToFetch,$.context(this, 'loadSuccess'));
    });
    },
    
    • If this function is used several times, then the ajax request will be triggered several times too
    • The XenForo ajax should not be inside the loop
    • Advice: try to only use the '$' character with jQuery object. For normal js variable, strip it, you're not in php. It will be easier and faster to re-read your code.
     

Share This Page