Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8912033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:05:59+00:00 2026-06-15T04:05:59+00:00

What is a good way to do some preprocessing on fields of a model.

  • 0

What is a good way to do some preprocessing on fields of a model. For example, suppose I have this data:

[{
        id: '1',
        sender: 'me',
        receiver: 'you',
        message: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum",
        date: new Date("October 13, 1975 11:13:00").toLocaleDateString()
    }]

In this simple model:

App.Models.Notification = Backbone.Model.extend({
    defaults: {
        'date': new Date()
    }
});

And I want to reduce the number of characters in the value of message in order to display it in a view, but only if that message contains more than a given number of characters. Maybe something like this:

if ( this.model.get('message').trim().split(" ").length > 30 ) {
  // code here
}

So, I don’t want to perform that preprocessing in all models. Should I do it in my view? If so, how? I have read a few solutions but some of them don’t apply to this situation and others seem like a hack.

Thanks!

UPDATE

Following Alex’s suggestion and as a reference, I put here an example of using Handlebars’ template helpers:

render: function() {

    Handlebars.registerHelper('getShort', function(options) {
        if ( options.fn(this).trim().split(" ").length > 30 ) {
            var message = options.fn(this);
            var shortText = $('<div/>', { text: message })
            .html()
            .trim()
            .substring(0, 200)
            .split(" ")
            .slice(0, -1)
            .join(" ") + "..."

            return shortText;
        }
        return options.fn(this);
    });

    template = Handlebars.compile( $("#my-template").html() );
    this.$el.html( template( this.model.toJSON() ));
    return this;
}

and using it like this:

index.html

{{#getShort}}{{message}}{{/getShort}}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:06:00+00:00Added an answer on June 15, 2026 at 4:06 am

    This kind of presentation logic should be put in template helpers, if your library stack supports them. If not, just add getShortenedMessage(length) method to model and use it to get your value. In any case, don’t change actual model attribute just to display it differently — this is bad design and can lead to all kinds of complications later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

XmlAttributeOverrides is a good way to override some attributes for serialization. MSDN says: You
I'm basically just doing some tests to figure out a good way to write
Wondering what the best / good way of doing this would be in jQuery.
What is a good way of writing something like this in one line. The
I have some data files from a legacy system that I would like to
I'm porting a 2D platformer and I need a good way of getting some
Why is there no batchSelect in JDBC? Is there some good way to handle
Is there is some good way to iterate through list in django template and
I was looking for some good way to parse mails with rails 3.2. I
I have some Scala code roughly analogous to this: object Foo { val thingA

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.