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

  • SEARCH
  • Home
  • 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 8539435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:21:54+00:00 2026-06-11T11:21:54+00:00

I would like to add dynamic properties in my model that is not in

  • 0

I would like to add dynamic properties in my model that is not in the REST service results. These dynamic properties would shorten names, format dates, etc. For example, my CanJS model like this:

var MyModel = can.Model({
    findAll: 'GET /Services/all'),
    findOne: 'GET /Services/{id}'),
    create:  'POST /Services/all'),
    update:  'PUT /Services/{id}'),
    destroy: 'DELETE /Services/{id}')
}, {});

Then I retrieve the data like this:

MyModel.findAll({}, function(data) {
    $('#main').html(can.view('templates/List.ejs'), data));
});

And this is how my List.ejs template looks like:

<% for(var i = 0; i < this.length; i++) { %>
    <div class="thumb"><img src="http://cdn.somewhere.com/images/logo.<%= this[i].BaseName %>.png" /></div>
    <div class="title"><%= this[i].Name %></div>
    <div class="date"><%= moment(this[i].StartDate).format('MMM DD, YYYY') %> - <%= moment(this[i].EndDate).format('MMM DD, YYYY') %></div>
    <div class="location"><%= this[i].LocationFriendly %></div>
    <div class="description"><%= this[i].Description %></div>
<% } %>

Notice the logic I am doing in the template for the image src and start/end dates. I would like to put this logic in the model so all I have to do in the template is this:

<% for(var i = 0; i < this.length; i++) { %>
    <div class="thumb"><img src="<%= this[i].ImageURL %>" /></div>
    <div class="title"><%= this[i].Name %></div>
    <div class="date"><%= this[i].StartDateFriendly %> - <%= this[i].EndDateFriendly %></div>
    <div class="location"><%= this[i].LocationFriendly %></div>
    <div class="description"><%= this[i].Description %></div>
<% } %>

How to I move this logic into the model layer or any better place then the template? Thanks for any help or advise.

  • 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-11T11:21:55+00:00Added an answer on June 11, 2026 at 11:21 am

    The easiest way would be to just create functions on your model:

    var MyModel = can.Model({
        findAll: 'GET /Services/all',
        findOne: 'GET /Services/{id}',
        create:  'POST /Services/all',
        update:  'PUT /Services/{id}',
        destroy: 'DELETE /Services/{id}'
    }, {
        imageUrl : function(){
            return "http://cdn.location.com/" + this.BaseName + ".png"
        },
        startDateFriendly : function(){
            return moment(this.StartDate).format('MMM DD, YYYY')
        },
        endDateFriendly : function(){
            return moment(this.StartDate).format('MMM DD, YYYY')
        },
        locationFriendly: function() {
            // ...
        }
    });
    

    Then you can just call these functions from the view:

    <% for(var i = 0; i < this.length; i++) { %>
        <div class="thumb"><img src="<%= this[i].imageUrl() %>" /></div>
        <div class="title"><%= this[i].Name %></div>
        <div class="date"><%= this[i].startDateFriendly() %> - <%= this[i].endDateFriendly() %></div>
        <div class="location"><%= this[i].locationFriendly %></div>
        <div class="description"><%= this[i].Description %></div>
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add dynamic content to front page of Magento site that
I would like to add to an element's position a dynamic amount e.g. 40px
I would like to add a horizontal seperating line on a dynamic populated table.
I would like to use jquery to build a dynamic add/ remove form. IT
I would like to add a toolbar button to CKEditor that shows the current
I would like to add dynamic content to a JQuery Accordion container. My code
I would like to create a combobox that binds to a dynamic resource and
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an

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.