Header

Tuesday, October 25, 2011

Visualforce Component IDs in JQuery

I bet most of you are using css classes in your vf components in order to select them using JQuery. I believe this the easiest workaround but not really that efficient especially if you have a number of vf components in your page and most of them should be using similar css classes. You are not able to use JQuery with the vf component's ID since it adds additional data to the ID you specified. The additional data denotes the component's position in the vf page structure. There is an easy way to refer to your vf components IDs without doing much code, however, just make sure that you don't have duplicate IDs or else this approach will fail. You may refer to your specific visualforce component by using this JQuery expression: $('[id$=YourVFComponentID]'). This will search all the IDs of the page components which contains the specified text after the id$.

Here's an example:

<apex:page>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script>
    $(function()
    {
        $('[id$=itDate]').datepicker();  
    });
</script>

    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputlabel value="Date:"/>
                    <apex:inputText id="itDate"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>

</apex:page>     

Thursday, October 20, 2011

Entity Framework Tidbit 3

At the beginning of my previous project, I was sending entity framework objects over the web service. After some time, I realized that it wasn't a good implementation since you had to reference the entity framework objects in the web service. In addition, the performance isn't great but good enough considering the convenience it brings to us developers. To address these issues, I decided to use business objects with my current implementation. This will remove the entity framework object references in the web services because I will sending business objects instead. The entity framework will only be present in my repository class which contains all the methods to the data layer of the application. More so, new data contracts are needed for every business object I decide to send in the web service. Here's some sample code to illustrate this implementation:

Business Object

namespace EntityDTO
{
    [DataContract(Namespace = "http://sb.services.betsson.com")]
    public class Customer
    {
        [DataMember]
        public int CustId {get; set;}
       
        [DataMember]
        public string CustName {get; set;}
    }
}

Repository

namespace EntityRepository
{
    public class CustomerRepo
    {
        private SBCustomerEntities _db;    

        public CustomerRepo()
        {
            _db = new SBCustomerEntities();
            _db.ContextOptions.LazyLoadingEnabled = false;
        }
       
        public List<Customer> GetAllCustomers()
        {
            List<Customer> lCustomers = new List<Customer>();
       
            var customers = from c in _db.tbl_customer select c;
           
            lCustomers.addRange(from customer in customers select new Customer{CustId=customer.customerID, CustName=customer.customerName});
           
            return lCustomers;
        }
    }
}

Web Service Interface

namespace Services
{
    public interface ISampleService
    {
        [OperationContract]
        List<Customer> GetCustomers();           
    }
}


Web Service Implementation

namespace Services
{
    public sealed class SampleService : ISampleService
    {
        List<Customer> GetCustomers()
        {
            CustomerRepo customerRepo = new CustomerRepo();
            var customers = customerRepo.GetAllCustomers();
           
            return customers;
        }
    }
}

Friday, October 7, 2011

Entity Framework Tidbit 2

I'll be discussing about custom associations in entity framework. You will need this when you want to create a link between two tables which are related but no relation constraints defined. Usually, this happens for tables whose relationship to one another is not really that significant to the system. The relationship is only established on a per need basis only. To create this relationships in entity framework, you need to add a custom association between the tables.  I consider it custom since, when you add tables in the entity model which have relation constraints, the association between the tables is automatically created. Here's the steps on how to do this:

1. Right click at the table and select new association.


2. Uncheck the "Add foreign key properties to.."


3. Click on the line created linking the two tables you have associated and go to properties. Then, set the referential constraints for the two tables.








After that, you have created a custom association between two tables! :)