Last active
          August 29, 2015 14:01 
        
      - 
      
- 
        Save hbillings/80b44fb4f686b19ca891 to your computer and use it in GitHub Desktop. 
    Javascript refactor
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function Article(){ | |
| stuff = arguments[1]; | |
| } | |
| Article.prototype = { | |
| // make this DRY-compatible | |
| get_tags:function(path) { | |
| tags = []; | |
| for (word in stuff.split()) { | |
| sometags = Tags.get_all(path); | |
| for (tag in sometags) { | |
| if (word == tag) | |
| tags.push(word); | |
| } | |
| } | |
| return tags; | |
| } | |
| } | |
| // combine User & SystemTags into one abstract function | |
| function Tags() {}; | |
| Tags.prototype = { | |
| get_all:function(path) { | |
| // hoist the variable so there's something to assign to & return | |
| var result; | |
| jQuery.ajax({ | |
| 'url': path, | |
| success: function(text) { | |
| // then assignment can happen w/in ajax call | |
| result = text.split(','); | |
| } | |
| }); | |
| // and result can be returned from larger function | |
| return result; | |
| } | |
| } | |
| Article.prototype.user_tags = Article.prototype.get_tags('/tags'); | |
| Article.prototype.system_tags= Article.prototype.get_tags('/systemtags'); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment