elwayman02 / ember-tumblr

Ember-Data abstraction for the Tumblr API
http://github.jhawk.co/ember-tumblr/
MIT License
7 stars 1 forks source link

tumblr-text issue #183

Closed MrChriZ closed 6 years ago

MrChriZ commented 6 years ago

There seems to be a bug in the Android Tumblr app where it doesn't save the title of a blog entry as a title annoyingly!

I've submitted the bug to Tumblr but don't expect it to be fixed this millennium.

So looking at how I can create a work around fix it I'm thinking it shouldn't be too difficult to check if the title is blank and if so then look to see if there is a heading entry as the first line in the blog and if so pull it out and use it as the title...

However my understanding of Ember objects and how ember-tumblr works is limited and I've no idea how to manipulate the data. I was hoping I could take my sortedposts entry in my controller: this.set('sortedPosts',sort('posts', 'sortBy'));

and do something like:

this.get(sortedPosts).any = function(post) {
      console.log(post);
      if (post.title=='' && post.body.contains('<h1>')
      {
       //manipulate post
      }
 }

However I can't seem to get hold of the individual posts. I also tried: this.get('sortedPosts').forEach(function(post){ console.log(post); });

Either way my loop seems to be entered the correct number of times... however post is undefined. Any tips?! I'm aware this might be better suited to StackOverflow... but does also have some overlap with the extension... Cheers, Chris.

MrChriZ commented 6 years ago

Ah I'm making progress from working in my component js file: if (this.get('post').get('title')==undefined && this.get('post').get('body').includes('h1')) ..... :)

MrChriZ commented 6 years ago

This is what I ended up with.

 if (this.get('post').get('title')==undefined)
        {   
            var elements = $(this.get('post').get('body'));        
            var title = elements.filter('h1:first').text();
            if (title)
            {
                this.get('post').set('title',title);
                elements = elements.not('h1:first');                
                var html = $('<div>').append(elements).html();
                this.get('post').set('body',html);            
            }           
        };

This seems to be working well. I'm still intrigued as to how to make this work from the controller level.

elwayman02 commented 6 years ago

You can combine those gets to make it feel less ugly:

this.get('post.title') this.get('post.body') this.set('post.title', title') this.set('post.body', html)

This method you wrote could be run as an additional computed property that reads from sortedPosts in the controller:

cleanPosts: computed('sortedPosts', function () {
  return this.get('sortedPosts').map(function (post) {
    if (post.get('title')) {
      return post;
    }
    return this.removeTitleFromBody(post);
  });
})

...where removeTitleFromBody would contain the code inside your conditional above.

MrChriZ commented 6 years ago

Thanks Jordan - that helped immensely!